Can my MVC2 application specify route restrictions on query string parameters?

My MVC2 application uses a component that calls subsequent AJAX callbacks to the same action, which causes all kinds of unnecessary access and data processing on the server. The component provider suggests re-redirecting these subsequent requests to another action. Subsequent queries are different in that they have a specific query string, and I want to know if I can put restrictions on the query string in my route table.

For example, the original request contains a URL, for example http: // localhost / document / display / 1 . This may be due to the default route. I want to write my own route for handling URLs like http: // localhost / document / display / 1? VendorParam1 = blah1 & script = blah.js and http: // localhost / document / display / 1? VendorParam2 = blah2 & script = blah .js by defining the "provider" in the URL.

I tried the following, but he System.ArgumentException: The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character. 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" } }); 

Is it possible to write a route that takes into account the query string? If not, do you have any other ideas?


Update: Simply put, can I write routing restrictions so that http: // localhost / document / display / 1 is redirected to the DocumentController.Display action, but http: // localhost / document / display / 1? VendorParam1 = blah1 & script = blah. js redirected to VendorController.Display action? In the end, I would like that any URL whose query string contains a "provider" should be redirected to the VendorController.Display action.

I understand that the first URL can be processed by default, but what about the second? Can this be done at all? After many trial and error on my part, the answer seems to be "No."

+4
source share
3 answers

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

As in Dutch, here is an 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") }); 
+9
source

You do not need a route for this. It is already being processed by the default binder. Query string parameters will be automatically bound to valid arguments:

 public ActionResult Foo(string id, string script, string vendorname) { // the id parameter will be bound from the default route token // script and vendorname parameters will be bound from the request string ... } 

UPDATE:

If you do not know the name of the query string parameters that will be passed, you can skip them through:

 foreach (string key in Request.QueryString.Keys) { string value = Request.QueryString[key]; } 
+2
source

This post is old, but you could not write a route to your default route.

this will only result in route tracking using the "provider" in args

 routes.MapRoute( null, "Document/Display/{id}?{args}", new { controller = "VendorController", action = "OtherAction" }, new { args=@ ".*(vendor).*"}//believe this is correct regex to catch "vendor" anywhere in the args 

);

And it will catch the rest

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

I have not tried this and I am new to MVC, but I believe this should work? From what I understand, if the restriction does not match the route, it is not used. Thus, he will check the next route. Since your next route does not use any restrictions for the arguments, it must match the route.

I tried this and it worked for me.

-1
source

Source: https://habr.com/ru/post/907783/


All Articles