I'm using custom route mapping in following way:
context.MapRoute(
"CustomRoute",
"Area/{controller}/{id}",
new { controller = "Task", action = "Index", id = UrlParameter.Optional },
new { id = @"\d+", httpMethod = new HttpMethodConstraint("GET") }
);
With above route I'm going to skip action name, pass parameter via slash /
, so I expect to have URL in following way:
domain.com/Area/Task/982
It works good by entering URL manually. Now trying to do the same via @Html.BeginRouteForm
helper:
@using (Html.BeginRouteForm("CustomRoute", new { id = Model.ID }, FormMethod.Get))
{
@Html.TextBoxFor(m => m.ID)
<button class="btn btn-default btn-sm" type="submit">Get data</button>
}
Submitting form it is rendering url in a way: domain.com/Area/Task?ID=982
What is missing above? How can I achieve desired custom route?
id
which will be the original value of the model. But then you have a textbox for the same route parameter and its the value of the textbox which will be used. but a browser has no knowledge of routes (server side code) so the value is added as a query string. Yournew { id = Model.ID }
is pointless and should be removed. And if your want../Area/Task/982
then you need javascriptnew { id = Model.ID }
giving me exactly same result. Agree, using javascript I can generate any URL. Still trying to understand why custom route is not considered by the form.