我的MVC2应用程序可以在指定的查询字符串参数路由约束? [英] Can my MVC2 app specify route constraints on Query String parameters?

查看:182
本文介绍了我的MVC2应用程序可以在指定的查询字符串参数路由约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVC2应用程序使用,使得后续的AJAX调用回相同的动作,这会导致各类服务器上的不必要的数据访问和处理的组件。组件供应商建议我重新路由的后续请求不同的操作。随后的请求的区别在于他们有一个特定的查询字符串,我想知道我是否可以把限制在我的路由表查询字符串。

My MVC2 app uses a component that makes subsequent AJAX calls back to the same action, which causes all kinds of unnecessary data access and processing on the server. The component vendor suggests I re-route those subsequent requests to a different action. The subsequent requests differ in that they have a particular query string, and I want to know whether I can put constraints on the query string in my route table.

例如,初始请求到来时像一个URL 的http://本地主机/文件/显示/ 1 。这可以通过缺省路由进行处理。我想写一个自定义路由处理如的HTTP URL://本地主机/文件/显示/ 1 = vendorParam1&blah1放大器;脚本= blah.js 并的 HTTP://本地主机/文件/显示/ 1 = vendorParam2&blah2放大器;脚本= blah.js 通过检测中的URL供应商

For example, the initial request comes in with a URL like http://localhost/document/display/1. This can be handled by the default route. I want to write a custom route to handle URLs like http://localhost/document/display/1?vendorParam1=blah1&script=blah.js and http://localhost/document/display/1?vendorParam2=blah2&script=blah.js by detecting "vendor" in the URL.

我尝试以下,但它抛出一个 System.ArgumentException:'?'路由URL不能以'/'或'〜'字符开始,它不能包含一个。字

I tried the following, but it throws a System.ArgumentException: The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.:

routes.MapRoute(
   null,
   "Document/Display/{id}?{args}",
   new { controller = "OtherController", action = "OtherAction" },
   new RouteValueDictionary { { "args", "vendor" } });

我可以写一个接受查询字符串考虑的路线?如果没有,你有什么其他的想法?

Can I write a route that takes the query string into account? If not, do you have any other ideas?

更新:简单地说,我可以写布线约束,使得的http://本地主机/文件/显示/ 1 路由到 DocumentController.Display 行动,但的 HTTP://本地主机/文件/显示/ 1 = vendorParam1&blah1放大器;脚本= blah.js 被路由到 VendorController.Display 行动?最后,我想它的查询字符串包含供应商,以被路由到 VendorController.Display 行动的任何URL。

Update: Put simply, can I write routing constraints such that http://localhost/document/display/1 is routed to the DocumentController.Display action but http://localhost/document/display/1?vendorParam1=blah1&script=blah.js is routed to the VendorController.Display action? Eventually, I would like any URL whose query string contains "vendor" to be routed to the VendorController.Display action.

据我所知,第一个URL可以用默认路由来处理,但对于第二个?是否有可能在所有做到这一点?经过大量的试验和错误在我的部分,它看起来像答案是否。

I understand the first URL can be handled by the default route, but what about the second? Is it possible to do this at all? After lots of trial and error on my part, it looks like the answer is "No".

推荐答案

查询字符串参数的可以在使用的限制,但它默认情况下不支持。 <一href=\"http://glamour.tweakblogs.net/blog/6174/route-constraints-op-querystring-parameters-in-asp-punt-net-mvc.html\">Here你可以找到一篇文章,描述了如何在ASP.NET MVC 2实现这一点。

QueryString parameters can be used in constraints, although it's not supported by default. Here you can find an article describing how to implement this in ASP.NET MVC 2.

由于它是荷兰语,这里的执行情况。添加IRouteConstraint'类:

As it is in Dutch, here's the implementation. Add an 'IRouteConstraint' class:

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; 
    } 
}

现在你可以在你的路由使用:

Now you can use this in your routes:

routes.MapRoute("object-contact", 
    "{aanbod}", 
    /* ... */, 
    new { pagina = new QueryStringConstraint("some|constraint") });

这篇关于我的MVC2应用程序可以在指定的查询字符串参数路由约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆