0

I've got an issue with the ASP.NET MVC framework routing taking the URL route I need for a HttpHandler via its default route, and visa versa:

routes.MapRouteLowercase(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional });// Parameter defaults

I've tried ignoring the route I need:

routes.IgnoreRoute("api/{*pathInfo}");

If I add to the RouteTable before the MVC routes are added, all the MVC routes end up pointing to my "/api" route.

If I add them after, the routes are in the routing table but not recognised. I've tried using the RouteDebugger which does show my routes as matching, but they're never called.

The source is in this micro REST framework I'm writing, if that helps.

Chris S
  • 64,770
  • 52
  • 221
  • 239
  • I have tried this also: http://stackoverflow.com/questions/5942452/unable-to-map-an-httphandler-to-a-path-wildcard-mapping but with no luck – Chris S May 01 '12 at 12:23

2 Answers2

1

Do you need the MVC framework to do the routing for you?

If not, have a look at this article: Basic Routing for HttpHandler

It is a good alternative, if you need something quick and simple ;)

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
0

I've found a work around for it, but it doesn't explain why my Routes are chomping at the MVC routes when the urls do not start with "api/" at all.

The work around I'm using is to add a constraint to my routes which checks if it's a Url generation, and ignore it, and if it's MVC data being passed in:

// Constrain HTTP method, and an extra constraint so MVC urls aren't swallowed.
route.Constraints = new RouteValueDictionary();
route.Constraints.Add("httpMethod", new HttpMethodConstraint(new string[] { restAttribute.Method }));
route.Constraints.Add("MvcContraint", new IgnoreMvcConstraint());

RouteTable.Routes.Add(route);

The constraint:

public class IgnoreMvcConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (routeDirection == RouteDirection.UrlGeneration)
            return false;
        if (values.ContainsKey("controller") || values.ContainsKey("action"))
            return false;

        if (route.Url.StartsWith(RestHttpHandler._baseUrl))
            return true;
        else
            return false;
    }
}
Chris S
  • 64,770
  • 52
  • 221
  • 239