Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3984756
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:48:30+00:00 2026-05-20T05:48:30+00:00

My MVC2 app uses a component that makes subsequent AJAX calls back to the

  • 0

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.

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.

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?


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.

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”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:48:30+00:00Added an answer on May 20, 2026 at 5:48 am

    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.

    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") });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my MVC2 app that uses the Service - Repository pattern, how can I
I have an ASP.NET MVC2 application that uses a parent controller to setup specific
So I have an MVC 2 app that uses the Active Directory Membership Provider.
I have a simple MVC2 app that doesn't seem to Redirect correctly. The code
I'm building an app using Windows Azure Table. I've encapsulated all my calls to
I have an ASP.NET MVC 2 app that uses URLS such as: /rsvp/33c4cf68-a2fe-4c0f-9834-08838e0532c3 /rsvp/4f28dad7-b05c-4887-818f-b4ae664b7192
In an ASP.NET MVC2 app, we have the standard login action... if (ValidateUser(model.Email, model.Password)
I have a MVC2 web app without a database that is being hosted by
What are best practices concerning error handling in an ASP.NET MVC2 web app that
Using an .net MVC2 app is generating a string of HTML code that be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.