仅使用控制器和 urlParameter 的 MVC3 路由 - 未指定任何操作
更新:在我们最近的完全重建中,该问题已不再发生,但自我发布此问题以来,web.config 和 global.asax.cs 文件尚未更改。我仍然不确定是什么导致了我观察到的行为。
原帖: 在我正在开发的 Web 应用程序中,我们有两种可能的安装路径 - 一种用于新安装,另一种用于升级。根据选择的路径,安装程序调用的初始 URL 为
或
。问题是,显然我们的路由映射没有正确处理第二个 URL,即使它与所需的路由定义匹配(根据 Haack 的路由调试器),它最终也会做一些奇怪的事情。一些奇怪的事情:URL
被重写为
。 Haack 的路由调试器在路由表的“匹配当前请求”列中显示与默认路由匹配的 URL,但在报告“匹配路由”时却显示“不适用”。也没有报告路由数据 - 没有控制器,没有操作,也没有 urlParameter。此外,routedebugger 报告的生成 URL 是使用路由“NotFound”生成的 URL:
AppRelativeCurrentExecutionFilePath is the portion of the request that Routing acts on.
AppRelativeCurrentExecutionFilePath: ~/Install/
通过调用
可以轻松解决此特定问题,但我宁愿得到明确的信息的理解为什么会发生这种情况并解决根本原因而不是解决问题。下面是重要的代码:
Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.asp/{*pathInfo}");
//404 error page
routes.MapRoute("NotFound", "NotFound",
new { controller = "Navigation", action = "NotFound" }
);
// 404 device not found page
routes.MapRoute("DeviceNotFound", "DeviceNotFound",
new { controller = "Navigation", action = "DeviceNotFound" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new[] { "Nm.Web.Mvc.Controllers", "Nm.Web.UI" }
);
}
InstallController.cs:
[HttpGet]
public ActionResult Index(int? installStep = null, bool upgrade = false)
{
var ins = DependencyResolver.Current.GetService<IInstallService>();
if (installStep != null)
{
ins.InstallStep = (InstallStep)installStep;
}
if ((upgrade == true) && (ins.HasDefaultAdminPassword() == false))
{
ins.InstallStep = InstallStep.Finished;
return RedirectToAction("Login", "User");
}
if (isLocalServer() && ins.InstallStep == InstallStep.SetAdminPassword)
{
return RedirectToAction("SetAdminPassword");
}
return RedirectToAction("SelectPath");
}
我在 Install 控制器 Index 方法中设置了一个断点,在 Global.asax 的 Application_Error 方法中设置了另一个断点,但都没有被触发。我不太确定“NotFound”的内容来自哪里 - 我们的 web.config 中确实有此部分:
<customErrors mode="Off" defaultRedirect="~/Error" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>
但正如您所看到的,我们关闭了自定义错误,并且没有迹象表明它正在命中 ErrorController (我为了确定起见,在其 NotFound 方法中还有另一个断点)。
我尝试设置一个备用路由来查看是否可以捕获它,但即使路由调试器再次显示此路由与 URL 匹配,我仍然得到相同的结果。
routes.MapRoute(
"Install", // Route name
"Install/{upgrade}", // URL with parameters
new { controller = "Install", action = "Index", upgrade = UrlParameter.Optional }, // Parameter defaults
new[] { "Nm.Web.Mvc.Controllers", "Nm.Web.UI" }
);
有人有什么想法或建议吗?
Update: The problem has stopped occurring with our most recent full rebuild, but the web.config and global.asax.cs files haven't been changed since I posted this question. I'm still not sure what was causing the behavior I observed.
Original Post:
In the web application I'm working on we have two possible paths that follow installs - one that's for new installs, and one that's for upgrades. Depending on which path is chosen, then initial URL our installer calls is either <siteroot>/Install
or <siteroot>/Install?upgrade=true
. The problem is that apparently our route mappings don't correctly handle this second URL, and it winds up doing strange things even though it matches the desired route definition (according to Haack's route debugger). A few weird things: The url <siteroot>/Install?upgrade=true
gets rewritten as <siteroot>/Install/?upgrade=true
. Haack's routedebugger shows the URL matching the default route in the "matches current request" column of the routes table, but where it reports "Matched Route" it says "n/a." There's no route data reported either - no controller, no action, and no urlParameter. Also, the generated URL reported by routedebugger is Generated URL: <siteroot>/NotFound?upgrade=true using the route "NotFound"
, and yet the request does NOT match the "NotFound" route, and the Current request info shows (some of) the correct information:
AppRelativeCurrentExecutionFilePath is the portion of the request that Routing acts on.
AppRelativeCurrentExecutionFilePath: ~/Install/
This specific problem could easily be fixed by calling <siteroot>/Install/Index?upgrade=true
, but I'd rather get a clear understanding of why this is happening and fix the root cause rather than working around the problem. Here's the salient code:
Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.asp/{*pathInfo}");
//404 error page
routes.MapRoute("NotFound", "NotFound",
new { controller = "Navigation", action = "NotFound" }
);
// 404 device not found page
routes.MapRoute("DeviceNotFound", "DeviceNotFound",
new { controller = "Navigation", action = "DeviceNotFound" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new[] { "Nm.Web.Mvc.Controllers", "Nm.Web.UI" }
);
}
InstallController.cs:
[HttpGet]
public ActionResult Index(int? installStep = null, bool upgrade = false)
{
var ins = DependencyResolver.Current.GetService<IInstallService>();
if (installStep != null)
{
ins.InstallStep = (InstallStep)installStep;
}
if ((upgrade == true) && (ins.HasDefaultAdminPassword() == false))
{
ins.InstallStep = InstallStep.Finished;
return RedirectToAction("Login", "User");
}
if (isLocalServer() && ins.InstallStep == InstallStep.SetAdminPassword)
{
return RedirectToAction("SetAdminPassword");
}
return RedirectToAction("SelectPath");
}
I have one breakpoint set within the Install controller Index method, another within the Application_Error method in Global.asax, and neither are ever triggered. I'm not really sure where the "NotFound" stuff is coming from - we do have this section in our web.config:
<customErrors mode="Off" defaultRedirect="~/Error" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>
but as you can see, we have custom errors off, and there's no indication it's hitting the ErrorController either (I have another breakpoint within its NotFound method just to be sure).
I've tried setting up an alternate route to see if that would catch it, but even though the routedebugger once again shows this route matching the URL, I still get the same results.
routes.MapRoute(
"Install", // Route name
"Install/{upgrade}", // URL with parameters
new { controller = "Install", action = "Index", upgrade = UrlParameter.Optional }, // Parameter defaults
new[] { "Nm.Web.Mvc.Controllers", "Nm.Web.UI" }
);
Does anyone have any thoughts or suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论