ASP.NET MVC:传播通过所有ActionLinks查询参数的正确方法 [英] ASP.NET MVC: The right way to propagate query parameter through all ActionLinks
问题描述
在我的应用程序,键
查询字符串参数可用于授予访问某些动作/人次。结果
现在,我希望所有ActionLinks和表单自动包括此参数,如果当前的查询字符串present。
什么是做到这一点的正确方法?
我问的是正确的方式,因为我看到,建议改变这种或那种方式(可选择扩展方法/佣工,手动参数传递)的意见几种解决方案。这不是我要寻找一个解决方案。
更新:结果
最终的解决方案(基于MikeSW的前面回答): https://gist.github.com/1918893 。 p>
您可以用路由做,但你需要多一点infrastrcuture。事情是这样的。
公共类RouteWithKey:路线
{
//省略的东西
公众覆盖VirtualPathData GetVirtualPath(RequestContext的的RequestContext,RouteValueDictionary值)
{
values.Add(钥匙,requestContext.HttpContext.Request.QueryString [钥匙]);
VAR T = base.GetVirtualPath(RequestContext的,值);
返回吨;
}
}
当然你需要检索来自请求参数的关键和处理的情况下关键是在查询中的第二个参数,但关键这个apporach会自动添加到通过正常as.net MVC建造的每utel佣工
我只是碰巧使用自定义路径为一个应用程序,用于不同的目的,但我;已经与code以上的测试,并将其添加参数,所以至少在我的项目似乎正常工作。 p>
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 include this parameter if present in current query string.
What is the right way to do this?
I am asking about the right way because I saw several solutions that proposed changing the views in one way or another (alternative extension methods/helpers, manual parameter passing). This is not a solution I am looking for.
UPDATE:
Final solution (based on MikeSW's anwer): https://gist.github.com/1918893.
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.
这篇关于ASP.NET MVC:传播通过所有ActionLinks查询参数的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!