我正在使用RC2
使用URL路由:
routes.MapRoute(
"Error",
"{*url}",
new { controller = "Errors", action = "NotFound" } // 404s
);
Run Code Online (Sandbox Code Playgroud)
以上似乎照顾这样的请求(假设默认路由表由初始MVC项目设置):"/ blah/blah/blah/blah"
覆盖控制器本身中的HandleUnknownAction():
// 404s - handle here (bad action requested
protected override void HandleUnknownAction(string actionName) {
ViewData["actionName"] = actionName;
View("NotFound").ExecuteResult(this.ControllerContext);
}
Run Code Online (Sandbox Code Playgroud)
但是,之前的策略不处理对Bad/Unknown控制器的请求.例如,我没有"/ IDoNotExist",如果我请求这个,我从Web服务器获取通用404页面而不是我的404,如果我使用路由+覆盖.
最后,我的问题是: 有没有办法在MVC框架中使用路由或其他东西来捕获这种类型的请求?
或者我应该默认使用Web.Config customErrors作为我的404处理程序并忘记所有这些?我假设如果我使用customErrors,由于Web.Config对直接访问的限制,我必须在/ Views之外存储通用404页面.
我有一些基本代码来确定我的MVC应用程序中的错误.目前在我的项目,我有一个名为控制器Error与操作方法HTTPError404(),HTTPError500()和General().它们都接受一个字符串参数error.使用或修改下面的代码.将数据传递给Error控制器进行处理的最佳/正确方法是什么?我想尽可能提供强大的解决方案.
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Clear();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
switch (httpException.GetHttpCode())
{
case 404:
// page not found
routeData.Values.Add("action", "HttpError404");
break;
case 500:
// server error
routeData.Values.Add("action", "HttpError500");
break;
default:
routeData.Values.Add("action", "General");
break;
}
routeData.Values.Add("error", exception);
// clear error on server
Server.ClearError();
// at this point how to …Run Code Online (Sandbox Code Playgroud) 有没有办法让catch所有路由都提供静态文件?
看看这个http://blog.nbellocam.me/2016/03/21/routing-angular-2-asp-net-core/
我基本上想要这样的东西:
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller}/{action=Index}");
routes.MapRoute("spa", "{*url}"); // This should serve SPA index.html
});
Run Code Online (Sandbox Code Playgroud)
因此,任何与MVC控制器不匹配的路由都将起作用 wwwroot/index.html
这一定是以前被问过的,但是在阅读了这里、这里、这里和这里之后,我无法推断相关部分以使其工作。我正在将一个旧的 Web 表单网站改造成 MVC,并希望捕获特定的传入 HTTP 请求,以便我可以发出RedirectPermanent(以保护我们的 Google 排名并避免用户因 404 问题而离开)。
我不需要拦截所有传入请求或解析某些id值,而是需要拦截以.aspx文件扩展名结尾(或包含)的所有请求,例如
www.sample.com/default.aspx
www.sample.com/somedir/file.aspx
www.sample.com/somedir/file.aspx?foo=bar
Run Code Online (Sandbox Code Playgroud)
对 MVC 路由的请求应被忽略(只是正常处理)。
这是我到目前为止所拥有的,除了ASPXFiles路线从未被击中。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// never generates a match
routes.MapRoute(
name: "ASPXFiles",
url: "*.aspx",
defaults: new { controller = "ASPXFiles", action = "Index" }
);
// Used to process all other requests (works fine)
routes.MapRoute( …Run Code Online (Sandbox Code Playgroud)