2

When I request the following URL from my MVC application I constantly get redirected to the HomeController.

www.mymvcapp.com/?m=standardvalue&a=someothervalue&code=12345

Now I'm trying to overcome this by edditing the Global.asax.cs request routes, e.g.

routes.IgnoreRoute(".*m=standardvalue.*");

So what I expect as a result is whenever the URL contains "m=standardvalue" it won't take me to the HomeController. It could be I'm doing this totally wrong, someone that can point me in the good direction for solving my problem.

Additional info: The link doesn't look anything like MVC, thats because Apllication Request Routing (IIS7) catches this URL and translates it to a webserver running PHP.

1 Answer 1

1

What you need here is a route constraint. The constraint will return true if the requested URL has no target controller and contains a query string with a variable m that equals standardvalue.

public class DefaultWithoutParamsConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext,
                      Route route,
                      string parameterName,
                      RouteValueDictionary values,
                      RouteDirection routeDirection)
    {
        return values[parameterName] == null &&
               httpContext.Request.QueryString["m"] == "standardvalue";
    }
}

Now just add this constraint to your ignore list:

routes.IgnoreRoute(
    "{*controller}",
    new { controller = new DefaultWithoutParamsConstraint() }
);
Sign up to request clarification or add additional context in comments.

3 Comments

Great! This is working, so thanks for that. Next I want to add standardvalue1, standardvalue2.. so if you have an easy fix please let me know.
use a regex in the constraint to verify that m matches your expression
Thanks for the quick reply, I got it fixed by adding 2 propperties (parameter and value) to DefaultWithoutParamsConstraint() and loading it via lamda expression

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.