MVC RedirectToAction 指向错误的路径
MVC RedirectToAction heading to wrong path
我上下搜索了这么多,仍然遇到这个错误。我不能,我这辈子都明白为什么。
{"The view 'Error' or its master was not found or no view engine
supports the searched locations. The following locations were
searched:\r\n~/Views/Administrator/Error.aspx\r\n~/Views/Administrator/Error.ascx\r\n~/Views/Shared/Error.aspx\r\n~/Views/Shared/Error.ascx\r\n~/Views/Administrator/Error.cshtml\r\n~/Views/Administrator/Error.vbhtml\r\n~/Views/Shared/Error.cshtml\r\n~/Views/Shared/Error.vbhtml"}
这是捕获到自定义错误记录 class:
internal static class _Logger
{
internal static void Log(Exception ex)
{
string logPath = HttpContext.Current.Server.MapPath(@"\Log\" + DateTime.Now.Month + "_" + DateTime.Now.Year + "_log");
File.AppendAllText(logPath,
"Time:" + DateTime.Now + "\n"
+ ex.Message +"\n" + ex.InnerException + "\n" + ex.StackTrace);
}
}
从这里抛出:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected void Application_Error()
{
var ex = Server.GetLastError(); //RIGHT HERE CATCHES IT.
_Logger.Log(ex);
}
}
路由配置:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Administrator",
url: "{controller}/{action}",
defaults: new { controller = "Administrator", action = "Index" }
);
routes.MapRoute(
name: "Trials",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Trials", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Error",
url: "{controller}/{action}",
defaults: new { controller = "Error", action = "Index"}
);
}
我在这里错过了什么?
谢谢。
编辑
我删除了自定义授权属性,但仍然抛出此异常。让我感到困惑的是它从来没有碰到 Catch 块。
编辑 2
我回去在应用程序启动时做了一个断点。
上面编辑的是实际抛出异常的地方。
您的路由配置完全错误。可选段就像变量一样——任何值都可以。参见
出现问题是因为您的 Administrator
路由捕获了 URL 中包含 1 或 2 个分段的每个请求,这阻止了您的其他路由被命中。
有很多方法可以解决此问题,但最简单的方法是在 URL 中至少设置一个段,而不是使用所有变量。
routes.MapRoute(
name: "Administrator",
url: "Administrator/{action}",
defaults: new { controller = "Administrator", action = "Index" }
);
routes.MapRoute(
name: "Trials",
url: "Trials/{action}/{id}",
defaults: new { controller = "Trials", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Error",
url: "Error/{action}",
defaults: new { controller = "Error", action = "Index"}
);
我上下搜索了这么多,仍然遇到这个错误。我不能,我这辈子都明白为什么。
{"The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:\r\n~/Views/Administrator/Error.aspx\r\n~/Views/Administrator/Error.ascx\r\n~/Views/Shared/Error.aspx\r\n~/Views/Shared/Error.ascx\r\n~/Views/Administrator/Error.cshtml\r\n~/Views/Administrator/Error.vbhtml\r\n~/Views/Shared/Error.cshtml\r\n~/Views/Shared/Error.vbhtml"}
这是捕获到自定义错误记录 class:
internal static class _Logger
{
internal static void Log(Exception ex)
{
string logPath = HttpContext.Current.Server.MapPath(@"\Log\" + DateTime.Now.Month + "_" + DateTime.Now.Year + "_log");
File.AppendAllText(logPath,
"Time:" + DateTime.Now + "\n"
+ ex.Message +"\n" + ex.InnerException + "\n" + ex.StackTrace);
}
}
从这里抛出:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected void Application_Error()
{
var ex = Server.GetLastError(); //RIGHT HERE CATCHES IT.
_Logger.Log(ex);
}
}
路由配置:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Administrator",
url: "{controller}/{action}",
defaults: new { controller = "Administrator", action = "Index" }
);
routes.MapRoute(
name: "Trials",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Trials", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Error",
url: "{controller}/{action}",
defaults: new { controller = "Error", action = "Index"}
);
}
我在这里错过了什么?
谢谢。
编辑
我删除了自定义授权属性,但仍然抛出此异常。让我感到困惑的是它从来没有碰到 Catch 块。
编辑 2
我回去在应用程序启动时做了一个断点。
上面编辑的是实际抛出异常的地方。
您的路由配置完全错误。可选段就像变量一样——任何值都可以。参见
出现问题是因为您的 Administrator
路由捕获了 URL 中包含 1 或 2 个分段的每个请求,这阻止了您的其他路由被命中。
有很多方法可以解决此问题,但最简单的方法是在 URL 中至少设置一个段,而不是使用所有变量。
routes.MapRoute(
name: "Administrator",
url: "Administrator/{action}",
defaults: new { controller = "Administrator", action = "Index" }
);
routes.MapRoute(
name: "Trials",
url: "Trials/{action}/{id}",
defaults: new { controller = "Trials", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Error",
url: "Error/{action}",
defaults: new { controller = "Error", action = "Index"}
);