MVC3 Routing with just controller and urlParameter - no action specified
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 win开发者_运维百科ds 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?
精彩评论