1
 routes.MapRoute(
                 "RouteSample",
               "Controler/Action/{sampleId}",
               new { controller = "Controller", action = "Action", sampleId=     UrlParameter.Optional }
);

<a href="@Url.RouteUrl("RouteSample", new { sampleId= 5 })> sample </a>


ActionResult Action(var sampleId)
{
         // Here I always get sampleId as null. I didn't get any value
         // I always get value only by RouteData.Values["Id"].. and another doubt is I have parameter name is sampleId. But I can get data only by mentioning "Id" in RouteData values.. why is like that ?
}

I always get value only by RouteData.Values["Id"].. and another doubt is I have parameter name is sampleId. But I can get data only by mentioning "Id" in RouteData values.. why is like that ?

4
  • only the type of parameter which is getting passed on should be same name is never fixed, depends upon the user. :) Commented Oct 18, 2013 at 9:05
  • What URL does @Url.RouteUrl("RouteSample", new { sampleId= 5 } generates on the client? Commented Oct 18, 2013 at 9:54
  • How can you declare sampleId as var? Commented Oct 18, 2013 at 10:03
  • Do you have other routes defined? Looks another route is being picked up. Commented Oct 18, 2013 at 10:11

1 Answer 1

1

I think your routes.MapRoute's attributes are wrong. Try This.

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{sampleId}",
                defaults: new { controller = "Home", action = "Index", sampleId = UrlParameter.Optional }
            );
Sign up to request clarification or add additional context in comments.

2 Comments

No. By route name only I'm calling the action. It's going correctly. But for me sampleId only cannot be pass. It's always null. I can get data only by RouteData.Values["Id"]
I think @SanketS is referring to the fact that controller and action should be between brackets like {controller}. But I think this is something that you just missed in the sample code ..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.