带参数的Mvc RouteUrl

时间:2013-10-18 08:55:33

标签: asp.net-mvc asp.net-mvc-4

 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 ?
}

我总是只通过RouteData.Values [“Id”]获得价值..而另一个疑问是我有参数名称是sampleId。但我只能通过在RouteData值中提及“Id”来获取数据。为什么会这样?

1 个答案:

答案 0 :(得分:1)

我认为你的routes.MapRoute属性是错误的。 试试这个。

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