Marry RouteData.Values ​​with action parameters

I install RouteDatainside customAction Filter

filterContext.RouteData.Values.Add("accountId", accountId);
filterContext.RouteData.Values.Add("permission", (int)permission);

Then I have an action like the following

public ActionResult Get(Guid accountId, int permission, int id) {

}

Can I marry values RouteDatafor action parameters through a route? If so, how?

I would just like to use a url like

http://localhost/Users/Get/1

They route data and can be obtained using

Guid accountId = (Guid)RouteData.Values["accountId"];
Permission permission = (Permission)RouteData.Values["permission"];
+3
source share
2 answers

You must create a custom ValueProvider, not an action filter.
For more information see here .

+1
source

You can use the ActionParameters property:

public class MyActionFilterAttribute: ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.ActionParameters["accountId"] = Guid.NewGuid();
        filterContext.ActionParameters["permission"] = 456;
    }
}

and then:

public class HomeController : Controller
{
    [MyActionFilter]
    public ActionResult Index(Guid accountId, int permission, int id)
    {
        return Content(string.Format("accountId={0}, permission={1}", accountId, permission));
    }
}

and upon request the /home/index/123following is shown:

accountId=f72fddb8-1c35-467b-a479-b2668fd5b2ec, permission=456
+1
source

All Articles