【发布时间】:2015-01-16 21:31:31
【问题描述】:
我在 MVC 5 中的项目。我想处理 404。我做到了,但问题是
当我使用以下 URL 访问视图时,一切正常且符合预期:
http://localhost/Hotels/Index30701000000
但是当我使用以下 URL 访问视图时,我收到 404.0 错误消息(错误如下所示)
http://localhost/Hotels/Edit/09099999dfdfb
http://localhost/Hotels/Edit/090/20130701000000
错误:HTTP 错误 404.0 - 未找到您要查找的资源已被删除、名称已更改或暂时不可用。
我的代码是
控制器
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>
【问题讨论】:
-
我不知道如何在
C#中执行此操作,但是您为什么不编辑您的服务器设置,以便将每个404重定向到您的自定义页面? -
我不知道该怎么做,
标签: c# asp.net asp.net-mvc-5 http-status-code-404 custom-error-pages