ASP.NET MVC: The right way to propagate query parameter through all ActionLinks

前端 未结 6 2302
礼貌的吻别
礼貌的吻别 2020-12-15 13:27

In my application, key query string parameter can be used to grant access to certain actions/views.
Now I want all ActionLinks and Forms to automatically in

6条回答
  •  被撕碎了的回忆
    2020-12-15 14:20

    You can do it with routes but you need a bit more infrastrcuture. Something like this

    public class RouteWithKey:Route
    {
      //stuff omitted
       public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
       {
         values.Add("key", requestContext.HttpContext.Request.QueryString["key"]);   
        var t = base.GetVirtualPath(requestContext, values);        
            return t;
        } 
    }
    

    Of course you'll need to retrieve the key from the request params and handle the case where key is the second parameter in the query, but this apporach will add automatically the key to every utel constructed via the normal as.net mvc helpers

    I just happen to use a custom route for an application, for a different purpose but I;ve tested with the code above and it adds the parameter, so at least in my project seems to work properly.

提交回复
热议问题