【问题标题】:ASP.NET MVC 5 - (HTTP Error 404.0 - Not Found) with long non-existing URLASP.NET MVC 5 - (HTTP 错误 404.0 - 未找到)具有长的不存在的 URL
【发布时间】:2013-12-20 09:31:15
【问题描述】:

我在 Microsoft Visual Studio Express 2013 for Web 中创建了一个新项目。 它是一个 ASP.NET MVC 5 - .NET Framework 4.5 项目。

我想处理(找不到资源):

我确实使用下面的代码处理了它。

如果我执行 (/Home/kddiede/ddiij) 或 (/djdied/djie/djs) 之类的操作,此代码将起作用,这将导致显示我的自定义错误页面。

但是,当我尝试执行 (/Home/kddiede/ddiij/dfd/sdfds/dsf/dsfds/fd) 之类的操作或任何不存在的长 URL 时,它会显示以下内容:

代码来自:http://www.codeproject.com/Articles/635324/Another-set-of-ASP-NET-MVC-4-tips

提示 16:自定义错误屏幕

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

Web.config

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

全球.asax

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

ErrorConfig 类

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

错误控制器

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 中

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

有人知道怎么解决吗?

提前致谢。

【问题讨论】:

    标签: asp.net-mvc url http-status-code-404 asp.net-routing


    【解决方案1】:

    解决了。要将所有不存在的 url 指向您的错误页面,请执行以下操作:

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

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

    【讨论】:

    • 我在让这个解决方案工作时遇到问题,这是因为我的操作上有一个 [Route()] 属性,所以 MVC 以不同的方式映射它。
    • 我在网上做了一个研究。人们不建议使用runAllManagedModulesForAllRequest = "true"。难道没有什么办法只需要使用模块吗?
    【解决方案2】:

    另一种方法是将其添加到 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>
    

    【讨论】:

    • 这从用户的角度来看是可行的,但实际上并没有为未找到的页面创建路由,这些页面可能与网站框架的其余部分一起工作,也可能不工作。它还会在访问无效 URL 时引发 Application_Error。
    猜你喜欢
    • 2015-01-16
    • 2011-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2014-06-16
    • 2014-10-18
    • 1970-01-01
    相关资源
    最近更新 更多