简体   繁体   中英

asp.net mvc 4.6 access all route dictionary parameters and values from controller action method

I have the following code

public class UserProfileController : Controller 
{
  public ActionResult Index(string manager="",string status="") 
  {
    var rvd = // Get route value dictionary for current call
    UserProfileService.GetEmployees(rvd);
    return View(vm);
  }
}

I would like to be able to pass a RouteValueDictionary to UserProfileService that includes key\\values for manager and status.

eg

If I made the following request, https://localhost:44301/UserProfile?manager=bill&status=active I would expect the route value dictionary to contain manager and status keys.

So far all I can find is RouteDate with key\\values for "controller" and "action" but not action method parameter values "manager","active".

ended up creating and extension method to do the job.

public static IDictionary<string,string> MergeRouteQueryValues (this Controller c) {
  var d = new Dictionary<string,string>() {
    {"controller",c.ControllerName()},
    {"action",c.ActionName()}
  };
  foreach (string key in c.Request.QueryString.Keys) { 
    d.Add(key,c.Request.QueryString[key]);
  }
  return d;
}

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