简体   繁体   中英

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

How do I modify the UrlAction to show this way?

You could modify your default route registration in Global.asax so that the {id} token is not part of your urls. 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.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.

 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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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