ASP.NET MVC 5 - (HTTP错误404.0 - 未找到),长不存在的网址 [英] ASP.NET MVC 5 - (HTTP Error 404.0 - Not Found) with long non-existing URL

查看:657
本文介绍了ASP.NET MVC 5 - (HTTP错误404.0 - 未找到),长不存在的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的Web微软的Visual Studio前preSS 2013的新项目。
这是一个ASP.NET MVC 5 - .NET框架4.5的项目

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):

我没有使用下面的code处理。

I did handle it using the code below.

如果我做这样的事情code就可以了(/首页/ kddiede / ddiij)或(/ djdied / djie / DJ们),这将导致显示我的自定义错误页。

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.

然而,当我尝试做一些像(/主页/ kddiede / ddiij / DFD / sdfds / DSF / dsfds / FD),或长期不存在的网址,它会告诉我:

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:<一href=\"http://www.$c$cproject.com/Articles/635324/Another-set-of-ASP-NET-MVC-4-tips\">http://www.$c$cproject.com/Articles/635324/Another-set-of-ASP-NET-MVC-4-tips

16提示:自定义错误的屏幕

Tip 16: Customizing error screens

错误页面是在/View/Shared/Error.cshtml

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

Web.config文件

Web.config

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

Global.asax中

Global.asax

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

ErrorConfig类

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

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

从上面我在Global.asax中没有添加,因为它已经在FilterConfig.cs提到的网站code最后这

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?

先谢谢了。

推荐答案

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

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.

    这篇关于ASP.NET MVC 5 - (HTTP错误404.0 - 未找到),长不存在的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆