dsw*_*tik 116 asp.net asp.net-mvc custom-error-pages asp.net-mvc-routing http-status-code-404
当有人键入不调用ASP.NET MVC中的有效操作或控制器的URL而不显示通用的"未找到资源"ASP.NET错误时,我试图创建自定义HTTP 404错误页面.
我不想使用web.config来处理这个问题.
是否有任何类型的路由魔法可以捕获任何无效的URL?
更新:我尝试了给出的答案,但我仍然得到丑陋的"资源未找到"消息.
另一个更新:好的,显然RC1中发生了一些变化.我甚至尝试过专门捕获404 HttpException,它仍然只是给了我"资源未找到"页面.
我甚至使用过MvcContrib的资源功能,没有 - 同样的问题.有任何想法吗?
And*_*ich 104
我试图在生产服务器上启用自定义错误3个小时,似乎我找到了最终解决方案如何在没有任何路由的ASP.NET MVC中执行此操作.
要在ASP.NET MVC应用程序中启用自定义错误,我们需要(IIS 7+):
在Web配置下的system.web部分下配置自定义页面:
<customErrors mode="RemoteOnly" defaultRedirect="~/error">
<error statusCode="404" redirect="~/error/Error404" />
<error statusCode="500" redirect="~/error" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
RemoteOnly意味着在本地网络上你会看到真正的错误(在开发过程中非常有用).我们还可以为任何错误代码重写错误页面.
设置魔术响应参数和响应状态代码(在错误处理模块或错误句柄属性中)
HttpContext.Current.Response.StatusCode = 500;
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
Run Code Online (Sandbox Code Playgroud)在以下system.webServer部分的Web配置中设置另一个魔术设置:
<httpErrors errorMode="Detailed" />
Run Code Online (Sandbox Code Playgroud)这是我发现的最后一件事,在此之后我可以在生产服务器上看到自定义错误.
Jac*_*mit 41
我通过创建一个返回本文中视图的ErrorController来使我的错误处理工作.我还必须在global.asax中添加"Catch All"到路由.
如果它不在Web.config中,我无法看到它将如何到达任何这些错误页面.?我的Web.config必须指定:
customErrors mode="On" defaultRedirect="~/Error/Unknown"
Run Code Online (Sandbox Code Playgroud)
然后我还补充说:
error statusCode="404" redirect="~/Error/NotFound"
Run Code Online (Sandbox Code Playgroud)
Yas*_*ser 27
NotFoundMVC - 只要在ASP.NET MVC3应用程序中找不到控制器,操作或路由,就会提供用户友好的404页面.将呈现名为NotFound的视图,而不是默认的ASP.NET错误页面.
您可以使用以下命令通过nuget添加此插件:Install-Package NotFoundMvc
NotFoundMvc在Web应用程序启动期间自动安装.它处理ASP.NET MVC通常抛出404 HttpException的所有不同方式.这包括缺少控制器,动作和路线.
分步安装指南:
1 - 右键单击您的项目并选择Manage Nuget Packages ...
2 - 搜索NotFoundMvc并安装它.

3 - 安装完成后,将向项目中添加两个文件.如下面的屏幕截图所示.

4 - 打开Views/Shared中新添加的NotFound.cshtml,并根据您的意愿修改它.现在运行应用程序并输入一个不正确的URL,您将看到一个用户友好的404页面.

没有更多,用户会得到错误消息 Server Error in '/' Application. The resource cannot be found.
希望这可以帮助 :)
PS:感谢Andrew Davey制作了这么棒的插件.
Ami*_*ila 20
在web.config中尝试此操作以替换IIS错误页面.这是我猜的最佳解决方案,它也会发出正确的状态代码.
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="Error404.html" responseMode="File" />
<error statusCode="500" path="Error.html" responseMode="File" />
</httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
来自Tipila的更多信息- 使用自定义错误页面ASP.NET MVC
Suj*_*ewa 15
此解决方案不需要web.config文件更改或catch-all路由.
首先,创建一个这样的控制器;
public class ErrorController : Controller
{
public ActionResult Index()
{
ViewBag.Title = "Regular Error";
return View();
}
public ActionResult NotFound404()
{
ViewBag.Title = "Error 404 - File not Found";
return View("Index");
}
}
Run Code Online (Sandbox Code Playgroud)
然后在"Views/Error/Index.cshtml"下创建视图;
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<p>We're sorry, page you're looking for is, sadly, not here.</p>
Run Code Online (Sandbox Code Playgroud)
然后在Global asax文件中添加以下内容,如下所示:
protected void Application_Error(object sender, EventArgs e)
{
// Do whatever you want to do with the error
//Show the custom error page...
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Error";
if ((Context.Server.GetLastError() is HttpException) && ((Context.Server.GetLastError() as HttpException).GetHttpCode() != 404))
{
routeData.Values["action"] = "Index";
}
else
{
// Handle 404 error and response code
Response.StatusCode = 404;
routeData.Values["action"] = "NotFound404";
}
Response.TrySkipIisCustomErrors = true; // If you are using IIS7, have this line
IController errorsController = new ErrorController();
HttpContextWrapper wrapper = new HttpContextWrapper(Context);
var rc = new System.Web.Routing.RequestContext(wrapper, routeData);
errorsController.Execute(rc);
Response.End();
}
Run Code Online (Sandbox Code Playgroud)
如果在执行此操作后仍然出现自定义IIS错误页面,请确保在Web配置文件中注释掉(或清空)以下部分:
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpErrors>
</httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
Ale*_*ort 10
只需在路由表的末尾添加catch all route并显示您想要的任何页面.
请参阅:如何捕获所有路由以处理ASP.NET MVC的"未找到404页面"查询?
小智 5
如果您在MVC 4中工作,您可以观看此解决方案,它对我有用.
将以下Application_Error方法添加到我的Global.asax:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Server.ClearError();
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
routeData.Values.Add("exception", exception);
if (exception.GetType() == typeof(HttpException))
{
routeData.Values.Add("statusCode", ((HttpException)exception).GetHttpCode());
}
else
{
routeData.Values.Add("statusCode", 500);
}
IController controller = new ErrorController();
controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
Response.End();
Run Code Online (Sandbox Code Playgroud)
控制器本身非常简单:
public class ErrorController : Controller
{
public ActionResult Index(int statusCode, Exception exception)
{
Response.StatusCode = statusCode;
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
在GitHub上查看Mvc4CustomErrorPage的完整源代码.
| 归档时间: |
|
| 查看次数: |
146607 次 |
| 最近记录: |