Question

I'm having a problem using iModelBinder with url in the format of

http://localhost/controller/action/id/value

the action would be the function in the controller the id/value is ie. id=12

When I try the above link i receive a 404 error page not found, and looking at the stack I can understand that MVC is looking for a path it does not understand.

using the following works

http://localhost/controller/action?id=value

If anyone as any idea if this problem can be resolved, I would really like to be able to use "/" as separators.

Vince

Was it helpful?

Solution

The url should really be in the format:

http://localhost/controller/action/id

For example:

http://localhost/products/index/1

And the id should then be specified in the controller action. For example:

public ActionResult Index(int id)
{
    ...

The route specified in the global.asax file will specify the format of the url. For the above url the default route will suffice:

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

Then the default model binder will automatically bind your id (i.e. 1 in the above url) to the int id in the action.

Like Adam was suggesting, I don't think you should specify the name of the id in the url as it is automatically bound to for you by the default model binder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top