0

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?

4
  • You have a route value for a property named 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. Your new { id = Model.ID } is pointless and should be removed. And if your want ../Area/Task/982 then you need javascript
    – user3559349
    Commented Jul 26, 2017 at 10:33
  • Removing new { 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.
    – mbigun
    Commented Jul 26, 2017 at 10:45
  • 2
    Because a browser does not know anything about your routes (that is server side code). A form that makes a GET will always add the values of its inputs as query string values.
    – user3559349
    Commented Jul 26, 2017 at 10:47
  • Ok. Clear, it makes sense.
    – mbigun
    Commented Jul 26, 2017 at 10:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.