简体   繁体   English

HTTP错误404.0 - 在MVC 5中找不到

[英]HTTP Error 404.0 - Not Found in MVC 5

My project in MVC 5.i wanted to handel 404 . 我在MVC 5.i的项目想要handel 404 i did it but problem is that 我做到了,但问题是

When I access the view using the following URL, everything works fine and as expected: 当我使用以下URL访问视图时,一切正常并且符合预期:

http://localhost/Hotels/Index30701000000

But when I access the view using following URL, I get 404.0 error message (error shown below) 但是当我使用以下URL访问视图时,我收到404.0错误消息(错误如下所示)

http://localhost/Hotels/Edit/09099999dfdfb                       
http://localhost/Hotels/Edit/090/20130701000000

Error: HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. 错误:HTTP错误404.0 - 未找到您要查找的资源已被删除,名称已更改或暂时不可用。

My code is 我的代码是

Controller 调节器

public class ErrorController : Controller
{
    // GET: Error
    public ActionResult Unauthorized()
    {
        Response.StatusCode = 404;
        Response.TrySkipIisCustomErrors = true;
        return View();
    }
}

RouteConfig RouteConfig

    routes.MapRoute(
        name: "Unauthorized",
        url: "Unauthorized/{action}/{id}",
        defaults: new
        {
            controller = "Error",
            action = "Unauthorized",
            id = UrlParameter.Optional
        }
    );

Global.asax Global.asax中

    protected void Application_Error()
    {
        Exception exception = Server.GetLastError();
        HttpException httpException = exception as HttpException;
    }

webconfig webconfig

  <system.web>
    <customErrors mode="On" defaultRedirect="Error">
      <error statusCode="404" redirect="Unauthorized" />
    </customErrors>
  </system.web>

View //Unauthorized.cshtml 查看 //Unauthorized.cshtml

@model System.Web.Mvc.HandleErrorInfo
@{
    ViewBag.Title = "Unauthorized";
}
<h1>Page Not Found!</h1>

Reference Link 参考链接

Another approach would be to define your error pages in an ErrorController, then use httpError section under system.WebServer in your web.config. 另一种方法是在ErrorController中定义错误页面,然后在web.config中的system.WebServer下使用httpError部分。

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" />
  <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
  <remove statusCode="500" />
  <error statusCode="500" responseMode="ExecuteURL" path="/Error/Error" />
</httpErrors>

The code above is so you can test your custom page. 上面的代码是您可以测试您的自定义页面。 But in your development environment, you could replace Custom with errorMode="DetailedLocalOnly" and existingResponse="Auto" so that you can still see the details errors. 但是在开发环境中,您可以使用errorMode =“DetailedLocalOnly”和existingResponse =“Auto”替换Custom,这样您仍然可以看到详细信息错误。

I wrote a post about it recently, and it comes with a sample demo project available on GitHub you can play with. 我最近写了一篇关于它的帖子,它附带了一个可以在GitHub上使用的示例演示项目。 http://www.neptunecentury.com/Blogs/ASP-NET/MVC5/MVC-5-How-To-Show-Custom-Error-Pages http://www.neptunecentury.com/Blogs/ASP-NET/MVC5/MVC-5-How-To-Show-Custom-Error-Pages

just Write these lines of code in global.asax file of your application. 只需在应用程序的global.asax文件中编写这些代码行。

protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 404)
        {
            if ((!Request.RawUrl.Contains("style")) && (!Request.RawUrl.Contains("images")))
            {
                Response.Clear();
                if (Response.StatusCode == 404)
                {
                    Response.Redirect("/ControllerName/ActionName");
                }
            }
        }
    }

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

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