views:

54

answers:

2

I've got a very basic ASP.Net MVC project where I'd like to use a parameter name of id on one of my controller actions. From everything I've read that shouldn't be a problem but for some reason using a parameter name of id fails to get the value extracted from the query string but if I change it to any other different name it will work.

I only have a single route in my global.asx

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

My controller method is:

public ActionResult Confirm(string id)
{
     ....
}

A URL of http://mysite/customer/confirm/abcd works. A URL of http://mysite/customer/confirm?id=abcd fails.

If I change the controller method to:

public ActionResult Confirm(string customerID)
{
     ....
}

then a URL of http://mysite/customer/confirm?customerID=abcd works.

Is there something special about using "id" as a parameter in an ASP.Net MVC query string?

Update: Changed id from 1234 to abcd, my id's are actually strings.

+1  A: 

If you need to have id in query string, then don't create route with 'id' parameter. In case you have route "{controller}/{action}" then you can use public ActionResult Confirm(string id) as your controller method.

Routes don't care about query strings.

stej
+1  A: 

If you do not apply an id parameter (either querystring or POST), the system just ignores it, and you can remove the "id" parameter in your controller:

public ActionResult Confirm()

In your case, you would just stick with the id parameter. Why make an ugly customerID parameter, when id is "mapped" automatically?

This is an easy and simple example of the use of id parameter.

public ActionResult Confirm(int? id)
{
     if (id.HasValue && id.Value > 0) // check the id is actually a valid int
         _customerServer.GetById(id.Value);

    // do something with the customer

    return View();
}

This works too, for me. We're doing it in our application right now with a standard route:

public ActionResult Confirm(string id)
{
     if (!string.IsNullOrEmpty(id)) // check the id is actually a valid string
         _customerServer.GetByStringId(id);

    // do something with the customer

    return View();
}
Kordonme
That's effectively what I tried with Confirm(string id). The MVC engine works with id being an int? but not a string for some reason.
sipwiz
But you specified an example of the id being 1234 - that's an int :-) You could also make it "int id". Then it's not nullable. But, regardless, it should still work with the "id" being a string.I've updated the answer.
Kordonme
Are you able to use both URL formats: customer/confirm/abcd and customer/confirm?id=abcd if so what do you have for your route in global.asx?
sipwiz
No, I'm not able to do that. You're not able to use the names of the parameters, you're using in the global.asax as querystring parameters, too. But /customer/confirm/1234 looks a lot better ;-)
Kordonme