Initially this demo application I’m working on had just one page with a simple form to login
<form action="/Login/AuthenticateUser" method="post">
<label for="username_or_email">Username or email</label>
<input id="handle" name="handle" type="text" value="">
<label for="password">Password</label>
<input id="pass" name="pass" type="password" value="">
</form>
And 2 routes defined
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"User",
"{controller}/{handle}",
new { Controller = "User", action = "Index", handle = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Post", action = "Index", id = UrlParameter.Optional }
);
}
This was working without issue and has the following controller method associated
[HttpPost]
public ActionResult AuthenticateUser(FormCollection collection)
{
//some code here ...
return View("Index");
}
Later it was decided that I needed a route to view user information directly so I wanted something like http://localhost/User/handle
So I crafted a route and I can view this without issue. But the next time I attempted to login with the original form (above) it never hits the http post method in my controller (but instead the http get “Index” method). also to note – after this post occurs the url in the browser appears to be correct, in this case showing http://localhost/Login/AuthenticateUser.
Strange behavior undoubtedly related to the new route. Also to note – I don’t have a route defined for this auth user method.
Any way I can have these work together? In addition what caused this strange behavior? Is the route itself not correct?
Thank you in advance
The following route:
Is routing all your requests to the
Indexaction by default. You have not allowed for overriding theactionroute value, only thehandleroute value. So/Login/AuthenticateUsertakes you to theIndexaction ofLogincontroller withhandleasAuthenticateUser. The route below it will never be used because{controller}/{handle}covers pretty much all requests.I’m not sure why it worked the first time, but it should have never worked. Could be that you didn’t build the application after you added the route I quoted above.
Update:
If you want to use something as general as
{controller}/{string}, you’ll need to make sure that no other routes conflict with it and still have all other controllers/actions route properly. I would recommend against using something as general as{controller}/{string}, but one way around that is if you make your general routes look like this:And place this above the
{controller}/{string}. Again, having a very general route map to something very specific can cause a headache in the future.