asp.net-mvc-2 – 我的MVC2应用程序可以在查询字符串参数上指定
我的MVC2应用程序使用一个组件,使后续的
AJAX调用回到相同的操作,这会导致服务器上的各种不必要的数据访问和处理.组件供应商建议我将这些后续请求重新路由到不同的操作.后续请求的不同之处在于它们具有特定的查询字符串,我想知道是否可以在路由表中对查询字符串设置约束.
例如,初始请求带有像http://localhost/document/display/1这样的URL.这可以通过默认路由处理.我想通过检测URL中的“vendor”来编写自定义路由来处理像http://localhost/document/display/1?vendorParam1=blah1&script=blah.js和http://localhost/document/display/1?vendorParam2=blah2&script=blah.js这样的URL. 我尝试了以下方法,但它抛出了一个System.ArgumentException:路由URL不能以’/’或’?’字符开头,它不能包含’?’字符.: routes.MapRoute( null,"Document/Display/{id}?{args}",new { controller = "OtherController",action = "OtherAction" },new RouteValueDictionary { { "args","vendor" } }); 我可以编写一条考虑查询字符串的路由吗?如果没有,你还有其他想法吗? 更新:简单地说,我可以编写路由约束,使http://localhost/document/display/1路由到DocumentController.Display操作,但http://localhost/document/display/1?vendorParam1=blah1&script=blah.js路由到VendorController.Display操作吗?最后,我希望任何查询字符串包含“vendor”的URL都被路由到VendorController.Display操作. 我知道第一个URL可以由默认路由处理,但第二个呢?是否可以这样做?经过大量的试验和错误,看起来答案是“不”. 解决方法
QueryString参数可以在约束中使用,但默认情况下不支持.
Here您可以在ASP.NET MVC 2中找到描述如何实现它的文章.
就像荷兰语一样,这是实施.添加’IRouteConstraint’类: public class QueryStringConstraint : IRouteConstraint { private readonly Regex _regex; public QueryStringConstraint(string regex) { _regex = new Regex(regex,RegexOptions.IgnoreCase); } public bool Match (HttpContextBase httpContext,Route route,string parameterName,RouteValueDictionary values,RouteDirection routeDirection) { // check whether the paramname is in the QS collection if(httpContext.Request.QueryString.AllKeys.Contains(parameterName)) { // validate on the given regex return _regex.Match(httpContext.Request.QueryString[parameterName]).Success; } // or return false return false; } } 现在您可以在路线中使用它: routes.MapRoute("object-contact","{aanbod}",/* ... */,new { pagina = new QueryStringConstraint("some|constraint") }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 为什么网站项目中缺少global.asax
- asp.net-mvc – 检查Viewbag属性是否为空,并在视图中使用默
- asp.net-mvc – MVC Ajax.Beginform OnComplete / OnSucces
- 从ASP.Net Web服务json输出中删除“d”对象
- asp.net-mvc – ASP.net MVC – 呈现包含不同类型的List,每
- asp.net-mvc – 在发送到视图之前如何修改控制器动作中的表
- asp.net – WSDL.exe工具的/ serverInterface和/ server选项
- stored-procedures – 使用经典的asp从oracle 11g存储过程返
- asp.net – 实体框架MVC慢页面加载
- asp.net-mvc-5 – Sharepoint 2013 MVC 5提供商托管的应用程