ASP.NET MVC – 如何抛出与StackOverflow类似的404页面
|
我目前有一个继承自System.Web.Mvc.Controller的BaseController类.在该类上,我有HandleError属性,将用户重定向到“500 – 糟糕,我们搞砸了”页面.这正在按预期工作. 这个工作 <HandleError()> _ Public Class BaseController : Inherits System.Web.Mvc.Controller ''# do stuff End Class 我也有我的404页面工作在一个Per-ActionResult基础上再次按预期工作. 这个工作 Function Details(ByVal id As Integer) As ActionResult
Dim user As Domain.User = UserService.GetUserByID(id)
If Not user Is Nothing Then
Dim userviewmodel As Domain.UserViewModel = New Domain.UserViewModel(user)
Return View(userviewmodel)
Else
''# Because of RESTful URL's,some people will want to "hunt around"
''# for other users by entering numbers into the address. We need to
''# gracefully redirect them to a not found page if the user doesn't
''# exist.
Response.StatusCode = CInt(HttpStatusCode.NotFound)
Return View("NotFound")
End If
End Function
再次,这是伟大的.如果用户输入的内容类似http://example.com/user/999(userID 999不存在),他们会看到相应的404页面,但URL不会更改(它们不会重定向到错误页面). 我无法理解这个想法 这是我遇到的问题.如果用户输入http://example.com/asdf-,他们将被踢到通用的404页面.我想做的是留下URL(IE:不重定向到任何其他页面),但只是显示“NotFound”视图,并将HttpStatusCode.NotFound推送到客户端. 例如,只需访问https://stackoverflow.com/asdf,您将在其中看到自定义的404页面,并查看保留的网址. 显然我错过了一些东西,但我无法想像出来.由于“asdf”实际上并不指向任何控制器,所以我的基本控制器类没有进入,所以我不能在那里的“HandleError”过滤器中. 先谢谢您的帮助. 注意:我绝对不想将用户重定向到404页面.我希望他们留在现有的URL,我想让MVC推送404 VIEW给用户. 编辑: 我也尝试过以下几点没有效果. Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.RouteExistingFiles = False
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.IgnoreRoute("Assets/{*pathInfo}")
routes.IgnoreRoute("{*robotstxt}",New With {.robotstxt = "(.*/)?robots.txt(/.*)?"})
routes.AddCombresRoute("Combres")
''# MapRoute allows for a dynamic UserDetails ID
routes.MapRouteLowercase("UserProfile",_
"Users/{id}/{slug}",_
New With {.controller = "Users",.action = "Details",.slug = UrlParameter.Optional},_
New With {.id = "d+"} _
)
''# Default Catch All Valid Routes
routes.MapRouteLowercase( _
"Default",_
"{controller}/{action}/{id}/{slug}",_
New With {.controller = "Events",.action = "Index",.id = UrlParameter.Optional,.slug = UrlParameter.Optional} _
)
''# Catch All InValid (NotFound) Routes
routes.MapRoute( _
"NotFound",_
"{*url}",_
New With {.controller = "Error",.action = "NotFound"})
End Sub
我的“NotFound”路线什么也没做. 解决方法找到我的答案 on my other SO question.非常感谢 Anh-Kiet Ngo的解决方案.protected void Application_Error(object sender,EventArgs e)
{
Exception exception = Server.GetLastError();
// A good location for any error logging,otherwise,do it inside of the error controller.
Response.Clear();
HttpException httpException = exception as HttpException;
RouteData routeData = new RouteData();
routeData.Values.Add("controller","YourErrorController");
if (httpException != null)
{
if (httpException.GetHttpCode() == 404)
{
routeData.Values.Add("action","YourErrorAction");
// We can pass the exception to the Action as well,something like
// routeData.Values.Add("error",exception);
// Clear the error,we will always get the default error page.
Server.ClearError();
// Call the controller with the route
IController errorController = new ApplicationName.Controllers.YourErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context),routeData));
}
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-3 – ASP.NET MVC 3和App_Code文件夹
- asp.net-mvc – 通过NuGet升级到Microsoft.AspNet.Mvc 5.0.
- asp.net-mvc – MVC4:禁用某些控制器上的移动视图
- asp.net-mvc – ASP.NET MVC路由和静态数据(即图像,脚本等)
- 为什么我得到“线程被中止”在asp.net?
- asp.net-mvc – 如果ASP.NET MVC中的用户匿名,则重定向到自
- 如何在ASP.NET自定义控件中持久保存List属性?
- ASP.NET Active Directory成员资格提供程序和SQL配置文件提
- asp.net-mvc-4 – 表单身份验证:角色(MVC 4)C#
- asp.net-mvc – 不一致的可访问性:DbContext中的属性类型
- asp.net – 与Entity Framework中的联结表有多对
- asp.net-web-api – 为什么我的超级简单的ASP.NE
- iis-7.5 – IIS 7.5 Siteminder不保护ASP.net MV
- ASP.NET和System.Diagnostics跟踪 – 我错过了什
- asp.net – 动态列在回发后消失
- asp.net-mvc – ASP.NET MVC 3 Razor模板VS Rend
- asp.net – 请求URL在IIS 7中无效
- asp.net下创建、查询、修改带名称空间的 XML 文件
- 在Kendo-UI图表中刷新方法和重绘方法有什么不同?
- asp.net-mvc-3 – 在查询字符串中使用“popup =
