简体   繁体   English

具有2个参数的MVC Url.Action

[英]MVC Url.Action with 2 parameters

I have the following in my controller where I am passing in 2 parameters: 我在控制器中传递了以下两个参数,具体如下:

    url = Url.Action("ViewReq ", "ProgramT ", new   System.Web.Routing.RouteValueDictionary(new { id = spid pgid = pid }), "http", Request.Url.Host);

When I view this, it shows up as: 当我查看此内容时,它显示为:

    http://localhost/Masa/ProgramT/ViewReq/20036?pgid=00001

I like it to show up as: 我喜欢它显示为:

http://localhost/Masa/ProgramT/ViewReq?id=20036&pgid=00001 http:// localhost / Masa / ProgramT / ViewReq?id = 20036&pgid = 00001

How do I modify the UrlAction to show this way? 如何修改UrlAction以这种方式显示?

You could modify your default route registration in Global.asax so that the {id} token is not part of your urls. 您可以在Global.asax修改默认路由注册,以使{id}令牌不属于您的网址。 Remove it or something. 将其删除。

I believe the Darin is correct. 我相信达林是正确的。

To get the URL that you desire, just keep your URL generation code the same 要获得所需的URL,只需保持URL生成代码相同即可

Url.Action("ViewReq ", "ProgramT ", new   System.Web.Routing.RouteValueDictionary(new { id = spid, pgid = pid }), "http", Request.Url.Host);

Then in the Global.asax file you add the following route below the default route. 然后在Global.asax文件中,在默认路由下方添加以下路由。

 routes.MapRoute(
       "YourNewRoute", // Route name
       "ProgramT/ViewReq/{id}/{pgid}", // URL with parameters
       new { controller = "ProgramT", action = "ViewReq",  id = UrlParameter.Optional, pgid = UrlParameter.Optional  } // Parameter defaults
   );

You should then see the URL as (assuming that id is 20036 and pgid is 00001) 然后,您应该看到该URL为(假设id为20036,而pgid为00001)

http://localhost/Masa/ProgramT/ViewReq/20036?pgid=00001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM