繁体   English   中英

找不到ASP.Net MVC 404文件自定义页

[英]ASP.Net MVC 404 File Not Found custom page

抱歉,如果问题不清楚,但是我需要“找不到文件”自定义页面,而不是“找不到页面”。 如果您在url .html的末尾键入-则不会通过Routes。

我有路线,可以捕捉所有路线:

routes.MapRoute(
            name: "Default",
            url: "{*url}",
            defaults: new { controller = "Home", action = "Index", url = UrlParameter.Optional },
            constraints: new { Domain = new DomainsConstraint() }
        );

但是,如何使找不到文件的自定义页面呢? 如果在和的网址中添加例如.html-它会向我显示找不到404文件

只需在web.config文件中设置错误配置

Web.config文件

 <customErrors mode="On" defaultRedirect="~/Error">
          <error redirect="~/Controller/Error" statusCode="404" />
    </customErrors>

调节器

public ActionResult Error(){
   var result = new FilePathResult("~/404.html", "text/html");
   return result;
}
routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "StaticContent", action = "PageNotFound" }
    );  

您可以创建自己的自定义错误页面:

<customErrors mode="On" defaultRedirect="~/Error">
      <error redirect="~/StaticContent/PageNotFound" statusCode="404" />
</customErrors>

public class StaticContent : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult PageNotFound()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View("PageNotFound");
    }
}

您可以从Global.asax文件进行管理。 您可以从这里得到这个答案的完美解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM