且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC 5 - (HTTP错误404.0 - 未找到),长不存在的网址

更新时间:2022-03-19 09:34:53

解决。指向所有不存在的网址你的错误页面,请执行以下操作:

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


  • 添加低于code。在您的RouteConfig.cs文件的末尾:

  • 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" }
    );
}


  • 添加code下面Web.config文件:

  • 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>
    


  • 这应该指向所有的不存在的网址,如(/ad/asd/sa/das,d/asd,asd.asd+dpwd'=12=2e-21)到你的错误页面。

    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.