🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)
🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 1 Min Lesezeit
0

[ASP.NET] 如何導向到錯誤頁面

↗ Quelle (dev.to)
🗣️ Stimme:

近期遇到客戶要求對網站進行弱點掃描,報告中其中一項是 Server Error Message,意思是當發生錯誤時,白底黃框的錯誤訊息會直接呈現給使用者,若是給一般使用者看到頂多得到觀感不佳的回饋,要是給 Hacker 看到,網站危險性一下就提升好幾個檔次,所以程式在運行過程發生錯誤時,應導向至錯誤畫面,而非暴露赤裸裸的錯誤資訊。



所以說,發生錯誤時要如何導向到錯誤頁面?自己寫 RedirectToAction?



其實 ASP.NET MVC 預設會在 FilterConfig.cs 中註冊 HandleErrorAttribute,也就是當發生 Http Status Code 500 時會交給 HandleErrorAttribute 去處理,它的功用是當程式部署後,倘若發生錯誤就會導向到 Error.cshtml,使用者就不會看到詳細錯誤資訊。不過在開發環境中還是可以看到詳細的錯誤資訊。




CODE
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
// 預設註冊
filters.Add(new HandleErrorAttribute());
}
}






要讓頁面成功導向到錯誤頁面,try catch 基本功不可少,當 Exception 發生時會呼叫 HandleErrorAttribute 裡的 OnException,OnException 會將畫面導向到預設的錯誤頁面,也就是 Error。有興趣的朋友可以喵一眼 OnException 的 Source Code




CODE
//
// Summary:
// Called when an exception occurs.
//
// Parameters:
// filterContext:
// The action-filter context.
//
// Exceptions:
// T:System.ArgumentNullException:
// The filterContext parameter is null.
public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}

if (!filterContext.IsChildAction && !filterContext.ExceptionHandled && filterContext.HttpContext.IsCustomErrorEnabled)
{
Exception exception = filterContext.Exception;
if (new HttpException(null, exception).GetHttpCode() == 500 && ExceptionType.IsInstanceOfType(exception))
{
string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
}






試著引發錯誤訊息讓畫面導向到 Error Page。




CODE
public ActionResult Index()
{
try
{
// 刻意引發錯誤訊息
throw new Exception();

return View();
}
catch (Exception ex)
{
throw;
}
}






打完,收工!






Thanks for reading the article 🌷 🌻 🌼



If you like it, please don't hesitate to click heart button ❤️

or follow my GitHub ⭐ I'd appreciate it.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Samsung Taps Mistral AI for On-Premises Chip Manufacturing
1 Quelle
CISA’s ChatGPT Incident Exposes a Bigger AI Governance Problem
1 Quelle
Beware — these new phishing attacks use a convincing fake Adobe Reader pages to trick victims into installing malware
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten [ASP.NET] 如何導向到錯誤頁面

Thematisch verwandte Begriffe: ASPNET, 如何導向到錯誤頁面 · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...