How to get the current route ID within a View from the URL (ASP.NET MVC)

前端 未结 8 1962
半阙折子戏
半阙折子戏 2021-01-30 02:27

Within the view that is returned from a URL such as /Controller/Action/1 (assuming the default route of controller/action/id), how can I get access to the ID from within the Vie

8条回答
  •  攒了一身酷
    2021-01-30 02:33

    We can pass id as Route Data or QueryString, so to support both of them i recommend this way:

    var routeValues=Url.RequestContext.RouteData.Values;
    var paramName = "id";
    var id = routeValues.ContainsKey(paramName)?
             routeValues[paramName]:
             Request.QueryString[paramName];
    

提交回复
热议问题