ASP.NET MVC 5 - (HTTP Error 404.0 - Not Found) with long non-existing URL

耗尽温柔 提交于 2019-11-27 01:11:30

问题


I created a new project in Microsoft Visual Studio Express 2013 for Web. It is an ASP.NET MVC 5 - .NET Framework 4.5 project.

I wanted to handle (The resource cannot be found):

I did handle it using the code below.

This code will work if I do something like (/Home/kddiede/ddiij) or (/djdied/djie/djs), this will result in showing my custom Error page.

However, when I try to do something like (/Home/kddiede/ddiij/dfd/sdfds/dsf/dsfds/fd), or any long non-existing URL, It will show me this:

Code from: http://www.codeproject.com/Articles/635324/Another-set-of-ASP-NET-MVC-4-tips

Tip 16: Customizing error screens

Error Page is in the /View/Shared/Error.cshtml

Web.config

<system.web>
<customErrors mode="RemoteOnly" />
</system.web>

Global.asax

protected void Application_EndRequest(Object sender, EventArgs e)
{
    ErrorConfig.Handle(Context);
}

ErrorConfig Class

public class ErrorConfig
{
  public static void Handle(HttpContext context)
  {
    switch (context.Response.StatusCode)
    {
      //Not authorized
      case 401:
        Show(context, 401);
        break;

      //Not found
      case 404:
        Show(context, 404);
        break;
    }
  }

  static void Show(HttpContext context, Int32 code)
  {
    context.Response.Clear();

    var w = new HttpContextWrapper(context);
    var c = new ErrorController() as IController;
    var rd = new RouteData();

    rd.Values["controller"] = "Error";
    rd.Values["action"] = "Index";
    rd.Values["id"] = code.ToString();

    c.Execute(new RequestContext(w, rd));   
  }
}

ErrorController

internal class ErrorController : Controller
{
  [HttpGet]
  public ViewResult Index(Int32? id)
  {
    var statusCode = id.HasValue ? id.Value : 500;
    var error = new HandleErrorInfo(new Exception("An exception with error " + statusCode + " occurred!"), "Error", "Index");
    return View("Error", error);
  }
}

This last bit of code from the website I mentioned above was not added in Global.asax because It is already in FilterConfig.cs

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
  filters.Add(new HandleErrorAttribute());
}

Anyone knows how to fix it?

Thanks in advance.


回答1:


Solved. To point all non-existing urls to your error page, do the following:

  • Add the code below at the end of your RouteConfig.cs file:

    public static void RegisterRoutes(RouteCollection routes)
    {
        // Default
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    
        // Add this code to handle non-existing urls
        routes.MapRoute(
            name: "404-PageNotFound",
            // This will handle any non-existing urls
            url: "{*url}",
            // "Shared" is the name of your error controller, and "Error" is the action/page
            // that handles all your custom errors
            defaults: new { controller = "Shared", action = "Error" }
        );
    }
    
  • Add the code below to your Web.config file:

    <configuration>
        <system.webServer>
           <modules runAllManagedModulesForAllRequests="true"></modules>
        </system.webServer>
    
        <system.web>
           <httpRuntime relaxedUrlToFileSystemMapping="true" />
        </system.web>
    </configuration>
    

That should point all the non-existing urls such as (/ad/asd/sa/das,d/asd,asd.asd+dpwd'=12=2e-21) to your error page.




回答2:


Another approach would be to add this to your web.config inside of the system.web element

<system.web>
<!-- ... -->

<!--Handle application exceptions-->
<customErrors mode="On">

  <!--Avoid YSOD on 404/403 errors like this because [HandleErrors] does not catch them-->
  <error statusCode="404" redirect="Error/Index" />
  <error statusCode="403" redirect="Error/Index" />
</customErrors>

</system.web>


来源:https://stackoverflow.com/questions/20366170/asp-net-mvc-5-http-error-404-0-not-found-with-long-non-existing-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!