简体   繁体   English

ASP.NET MVC如何在不重写URL的情况下处理Application_Error的404错误

[英]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

  • an ErrorsController.cs 一个ErrorsController.cs
  • a view Http404.cshtml 视图Http404.cshtml
  • and edited 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.

相关问题 如何从ASP.NET MVC 2中的Application_Error重定向? - How to redirect from Application_Error in ASP.NET MVC 2? 如何在Application_Error()中知道asp.net中的请求是ajax - How to know if the request is ajax in asp.net in Application_Error() ASP.NET MVC自定义错误处理Application_Error Global.asax? - ASP.NET MVC Custom Error Handling Application_Error Global.asax? asp.net中的Application_Error与Context_Error - Application_Error vs Context_Error in asp.net 您是否应该在ASP.NET MVC的global.asax中使用方法Application_Error? - Should you use the method Application_Error in your global.asax in ASP.NET MVC? 在ASP.NET MVC4中,application_error哪里去了? - In ASP.NET MVC4, where has application_error gone? Asp.net mvc 覆盖基础 controller 中的 OnException 不断传播到 Application_Error - Asp.net mvc override OnException in base controller keeps propagating to Application_Error ASP.NET MVC 5-错误处理-404页面未找到 - ASP.NET MVC 5 - Error Handle for - 404 Page not found .Net Mvc:如何为Application_Error()引发错误来管理它们? - .Net Mvc: How to fire a error for Application_Error() manage them? IIS 8.5上托管的404错误ASP.NET MVC应用程序 - 404 error ASP.NET MVC application hosted on IIS 8.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM