asp.net mvc 2 - Trouble Figuring Out The routes.Maproute For This URL (C#) -
despite plethora of url routing posts, have yet reach enlightenment on subject.
i have mvc app works fine, want fancy , url rewriting make easier on users.
the default route slight variation on usual:
routes.maproute( "default", "{controller}/{action}/{query}", new{ controller = "somecontroller", action="index", query = "" });
what want re-route url http://mysite.com/12345 details action of somecontroller 12345 query. tried this:
routes.maproute( "directtodetails", "{query}", new{ controller = "somecontroller", action="details" query="" }); routes.maproute( "default", "{controller}/{action}/{query}", new{ controller = "somecontroller", action="index", query = "" });
but ends obscuring http://mysite.com/ , goes somecontroller/details instead of somecontroller/index.
edit:
per advice of @jon removed query = "" in anonymous object:
routes.maproute( "directtodetails", "{query}", new{ controller = "somecontroller", action="details" });
... addressed question far went; when submit form @ views/someview/index (that submits aforementioned somecontroller/details) "resource cannot found" error.
here form declaration:
<% using (html.beginform( "details", "somecontroller", formmethod.post, new { id = "searchform" })) { %> <% html.renderpartial("searchform", model); %> <%= html.validationmessagefor(model => model.query) %> <% } %>
remove query = ""
(obviously it's not optional because of problem you're having - no need put in 3rd argument if there's no sensible default) , use overload of maproute
takes 4th parameter route constraints:
routes.maproute( "directtodetails", "{query}", new { controller = "somecontroller", action="details" }, new { query = @"\d+" });
obviously need adjust regex if it's not integer...
in response edit...make sure you've removed default query "short" route - you've posted default 1 there. suggest double check form , make sure it's posting value query
(maybe set break point on post action method , make sure query parameter being bound correctly?).
Comments
Post a Comment