Despite the plethora of URL routing posts, I have yet to reach enlightenment on the subject.
I have an MVC app that works fine, but I want to get fancy and do some URL rewriting to make it easier on my users.
The default route is a slight variation on the usual:
routes.MapRoute(
"Default",
"{controller}/{action}/{query}",
new{ controller = "SomeController", action="Index", query = "" });
What I want to do is re-route the URL http://mysite.com/12345 to the Details action of SomeController with the 12345 query. I 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 that ends up obscuring http://mysite.com/ and it goes to SomeController/Details instead of SomeController/Index.
EDIT:
per the advice of @Jon I removed the query = "" in the anonymous object:
routes.MapRoute(
"DirectToDetails",
"{query}",
new{ controller = "SomeController", action="Details" });
... which addressed the question as far as it went; now when I submit the form at Views/SomeView/Index (that submits to the aforementioned SomeController/Details) I get a "resource cannot be found" error.
Here is the form declaration:
<% using (Html.BeginForm(
"Details",
"SomeController",
FormMethod.Post,
new { id = "SearchForm" })) { %>
<% Html.RenderPartial("SearchForm", Model); %>
<%= Html.ValidationMessageFor(model => model.Query) %>
<% } %>