Byren Higgin
Byren Higgin

Reputation: 562

Parameters in MVC not being correctly passed into the view from routing

I'm teaching myself MVC and have an issue with routing correctly

I have a controller named "ClipsController" and 1 view inside with the name "Index" (boring, i know)

I have my routeconfig file configured with the following route:

routes.MapRoute(
    "Clips",
    "Clips/{id}",
    new { controller = "Clips", action = "Index", id = urlparameters.Optional }
);

which is Before the "Default" route. When i go to /Clips/ExampleID it does hit the correct route, and does start the Index action in the Clips controller. What i'm having trouble with is the parameter 'ID' fails to pass through into the Index page, but i end up on the index action of the Clips controller, with the URL domain.my/Clips/ExampleID

I attempt to get the ID parameter with

httpcontext.current.request.querystring["id"]

which always returns a null. My actionresult in the controller is as follows:

public ActionResult Index(string id)
{
    return view()
}

To reiterate, I'm not able to see the querystring id on the index view, even though the URL does the correct route, and the actionresult in the controller is to my kowledge correct. If i have done something critically wrong or you need more information please let me know, Thanks.

Upvotes: 2

Views: 2229

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

I attempt to get the ID parameter with

httpcontext.current.request.querystring["id"]

No, you don't have any query string parameters in this url:

http://domain.my/Clips/ExampleID

Query string parameters follow the ? character in an url. For example if you had the following url:

http://domain.my/Clips?id=ExampleID

then you could attempt to read the id query string parameter using your initial code.

With this url: http://domain.my/Clips/ExampleID you could query the id route value parameter. But using HttpContext.Current is absolutely the wrong way to do it. You should never use HttpContext.Current in an ASP.NET MVC application. Quite on the contrary, you can access this information everywhere where you have access to HttpContextBase (which is pretty much everywhere in the ASP.NET MVC application pipeline):

httpContext.Request.RequestContext.RouteData.Values["id"]

Long story short, if you needed to query the value of this parameter inside your controller action you would simply use the provided id argument:

public ActionResult Index(string id)
{
    // Here the id argument will map to ExampleID
    return view()
}

Also you probably don't need your custom route:

routes.MapRoute(
    "Clips",
    "Clips/{id}",
    new { controller = "Clips", action = "Index", id = urlparameters.Optional }
);

That's completely redundant and it is already covered by the default route:

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

So feel more than free to get rid of your custom route.

Upvotes: 4

Related Questions