1

I use the Url.Action method to generate a Url string.

enter image description here

Thats the result:

"/Home/GetRejectTest/0?IsSelected=False"

The controller and action name are correct but the query parameters are screwed up. Is this because The action does not have a RouteAttribute thus query parameters are generated?

My action:

   public ActionResult GetRejectTest(Test test)
        {
            return new EmptyResult();

        }

Test class has those 3 properties Id, Name, IsSelected.

My route definition:

 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
9
  • what is urlHelper ? Commented Jan 20, 2016 at 20:51
  • 1
    Show the action method signature and the relevant registered routes Commented Jan 20, 2016 at 20:52
  • Its System.Web.Mvc.UrlHelper Commented Jan 20, 2016 at 20:52
  • 2
    what is values ?. From your screenshot, it looks like the dictionary has 0 and null values for Name and Id. So whatever you are getting is the expected result. Isn't it ? Commented Jan 20, 2016 at 21:05
  • 3
    Assuming values is a new default instance of Test, (and your using the Default route {controller}/{action}/{id}), then the url is correct Commented Jan 20, 2016 at 21:14

1 Answer 1

5

The url your generating (/Home/GetRejectTest/0?IsSelected=False) is correct for your route definition. You passing a new instance of Test to the Url.Action() method, which

  1. Internally builds a dictionary based on the name and value (using.ToString()) of each property in your model - i.e. controller=Home, action=GetRejectTest, id=0, Name=null, IsSelected=False
  2. Then searches your route definitions for a match which it finds (url: "{controller}/{action}/{id}") and updates the placeholders (which at this point generates /Home/GetRejectTest/0) but your route definition does not have url parameters for Name and IsSelected so these are added as query string parameters (because Name is null, a query string for that propery is not generated) so the result is now /Home/GetRejectTest/0?IsSelected=False

You have not indicated what result you're actually expecting, but creating specific route definitions will solve most cases. For example if you want

/Home/GetRejectTest/0/false

or /Home/GetRejectTest/0/false/someName if the value of Name is not null, then you can create an additional route (which must be before the default route)

routes.MapRoute(
  name: "Test",
  url: "Home/GetRejectTest/{id}/{isselected}/{name}",
  defaults: new { controller = "Home", action = "GetRejectTest", name = UrlParameter.Optional }
);

Note that because Name is typeof string and therefore can be null, the {name} placeholder needs be the last one and marked as UrlParameter.Optional (otherwise it will revert back to using query string parameters)

Sign up to request clarification or add additional context in comments.

1 Comment

Ok now I understand it. That I used id as query param in myself was just stupid then ;-) Thanks for your reasonable explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.