[英]ASP.NET MVC how to handle 404 error from Application_Error without rewriting the URL
I have created a really simple ASP.NET MVC 5 application where i want to handle the 404 exception from my Application_Error
as shown in this question and in this other answer . 我创建了一个非常简单的ASP.NET MVC 5应用程序,在该应用程序中我想处理Application_Error
的404异常,如本问题和其他答案所示 。 But when i try to reach a page who don't exist (and expect my 404 page to be displayed) the source code of my custom error page is shown in plain text! 但是,当我尝试访问不存在的页面(并希望显示404页面)时,自定义错误页面的源代码将以纯文本显示! . 。
I don't want my URL to be rewrited as in this post 我不希望我的网址像这篇文章一样被重写
My project is really simple. 我的项目非常简单。 I just added to a basic ASP.NET WebApplication with Razor
: 我刚刚ASP.NET WebApplication with Razor
将其添加到基本的ASP.NET WebApplication with Razor
:
ErrorsController.cs
一个ErrorsController.cs
Http404.cshtml
视图Http404.cshtml
Global.asax
并编辑了Global.asax
as shown below: 如下所示:
Project organisation: 项目组织:
Global.asax: Global.asax中:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{}
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
HttpException httpException = exception as HttpException;
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Errors");
if (httpException == null)
{
routeData.Values.Add("action", "Index");
}
else
{
switch (httpException.GetHttpCode())
{
case 404:
routeData.Values.Add("action", "Http404");
break;
}
}
Response.Clear();
Server.ClearError();
Response.TrySkipIisCustomErrors = true;
IController errorController = new ErrorsController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
ErrorsController.cs: ErrorsController.cs:
public class ErrorsController : Controller
{
public ActionResult Http404(string url)
{
return View("Http404");
}
}
Http404.cshtml Http404.cshtml
@{
ViewBag.Title = "Page not found";
}
<h2>Page not found</h2>
But then when i try to reach a page who don't exist everything i see is the source code of my 404 page: output for a non existing page: 但是,当我尝试到达一个不存在的页面时,我看到的是我的404页面的源代码: 不存在的页面的输出:
I searched for hours in stackoverflow and other site but found nothing to help me here. 我在stackoverflow和其他站点中搜索了几个小时,但没有任何帮助。
Some people use very similar code to handle the 404 exception but don't have the same result. 有些人使用非常相似的代码来处理404异常,但结果却不同。 I'm really stuck on this, i hope someone could help me, or at least show me a better approach than this answer to handle 404 exceptions in ASP.NET MVC 5. 我真的很坚持这一点,希望有人可以帮助我,或者至少向我展示一种比此答案更好的方法来处理ASP.NET MVC 5中的404异常。
If you have plain text html in your browser, it may mean you have a wrong Content-Type
value in your header. 如果浏览器中包含纯文本html,则可能意味着标头中的Content-Type
值错误。
Try this : 尝试这个 :
Response.ContentType = "text/html";
it is certainly not the best solution, but it works. 它当然不是最好的解决方案,但它可以工作。
Ludo 鲁
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.