gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:39:36 26 4
gpt4 key购买 nike

我在 MVC 5.i 中的项目想要处理 404。我做到了,但问题是

当我使用以下 URL 访问 View 时,一切正常且符合预期:

http://localhost/Hotels/Index30701000000

但是当我使用以下 URL 访问 View 时,我收到 404.0 错误消息(错误如下所示)

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

错误:HTTP 错误 404.0 - 未找到您要查找的资源已被删除、名称已更改或暂时不可用。

我的代码是

Controller

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

路由配置

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

Global.asax

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

网络配置

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

查看//Unauthorized.cshtml

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

Reference Link

最佳答案

另一种方法是在 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>

上面的代码让您可以测试您的自定义页面。但是在您的开发环境中,您可以将 Custom 替换为 errorMode="DetailedLocalOnly"和 existingResponse="Auto",这样您仍然可以看到详细错误。

我最近写了一篇关于它的帖子,它在 GitHub 上提供了一个示例演示项目,您可以使用它。 http://www.neptunecentury.com/Blogs/ASP-NET/MVC5/MVC-5-How-To-Show-Custom-Error-Pages

关于c# - HTTP 错误 404.0 - 在 MVC 5 中找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27015585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com