asp.net-mvc ASP.NET MVC 5 -(HTTP 错误 404.0 - 未找到)具有不存在的长 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20366170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC 5 - (HTTP Error 404.0 - Not Found) with long non-existing URL
提问by NewUser_
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.
我在 Microsoft Visual Studio Express 2013 for Web 中创建了一个新项目。它是一个 ASP.NET MVC 5 - .NET Framework 4.5 项目。
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.
如果我执行 (/Home/kddiede/ddiij) 或 (/djdied/djie/djs) 之类的操作,此代码将起作用,这将导致显示我的自定义错误页面。
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:
但是,当我尝试执行类似 (/Home/kddiede/ddiij/dfd/sdfds/dsf/dsfds/fd) 或任何不存在的长 URL 时,它会显示以下内容:


Code from: http://www.codeproject.com/Articles/635324/Another-set-of-ASP-NET-MVC-4-tips
代码来自:http: //www.codeproject.com/Articles/635324/Another-set-of-ASP-NET-MVC-4-tips
Tip 16: Customizing error screens
技巧 16:自定义错误屏幕
Error Page is in the /View/Shared/Error.cshtml
错误页面位于 /View/Shared/Error.cshtml
Web.config
网页配置
<system.web>
<customErrors mode="RemoteOnly" />
</system.web>
Global.asax
全球.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
我上面提到的网站的最后一点代码没有添加到 Global.asax 中,因为它已经在 FilterConfig.cs 中
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
Anyone knows how to fix it?
有谁知道如何修复它?
Thanks in advance.
提前致谢。
回答by NewUser_
Solved. To point all non-existing urls to your error page, do the following:
解决了。要将所有不存在的 url 指向您的错误页面,请执行以下操作:
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>
在 RouteConfig.cs 文件末尾添加以下代码:
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" } ); }将以下代码添加到您的 Web.config 文件中:
<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.
这应该将所有不存在的 url,例如 (/ad/asd/sa/das,d/asd,asd.asd+dpwd'=12=2e-21) 指向您的错误页面。
回答by Andy Raddatz
Another approach would be to add this to your web.config inside of the system.webelement
另一种方法是将其添加到system.web元素内的 web.config
<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>

