I have a legacy project that has a single IHttpHandler implementing class that routes all the requests using a huge switch statement etc.. I am trying to introduce Attribute Routing with ApiControllers but the first one always has the priority. Is it possible to configure the system (either code or IIS) so that ApiControllers have priority over my single IHttpHandler implementing class? I put my AttributeRouting first and then there are all the aspx ones but still it is not getting processed first..no matter what I do (having them under the same project)

Edit: There is a IHttpModule that decides based on what is after api/ to route it to specific ashx file. One of them is the one described..

Thanks Simplified Code Architecture:

 //SolutionName/Api/MyModule.cs (Legacy Code)
 //this routes based on what is after api/ to Resource.ashx or other ashx files
 public class MyModule : IHttpModule {
    //Url Rewriting if needed[..]
 }

//SolutionName/API/Resource.ashx (Legacy Code)
//this is hit at any request solutionname/api/anything
public class DefaultHandler : IHttpHandler 
{
   public void ProcessRequest(HttpContext context) {
       String APIBranch = parse(context);
       switch(APIBranch)
       {
           case "endpoint1": methodOne(); break;
           case "endpoint2": methodTwo(); break;
           [...]
           default: throw Exception(); break;
       }
   }
}

//SolutionName/API/App_Start/AttributeRoutingHttpConfig.cs
public static class AttributeRoutingHttpConfig
{
    public static void RegisterRoutes(HttpRouteCollection routes) 
    {    
        // See http://github.com/mccalltd/AttributeRouting/wiki for more options.
        // To debug routes locally using the built in ASP.NET development server, go to /routes.axd

        routes.MapHttpAttributeRoutes();
    }

    public static void Start() 
    {
        RegisterRoutes(GlobalConfiguration.Configuration.Routes);
    }
}

//SolutionName/API/Controllers/MyController.cs
//this should have been hit for a GET on solutionname/api/endpoint3/id
[RoutePrefix("endpoint3")]
public class MyController : ApiController
{
    private IModelDao modelDao;

    MyController(IModelDao modelDao){
        this.modelDao = modelDao;
    }   

    [Route("{id}")]
    [HttpGet]
    public Model GetSomething(int id)
    {
        Model model = modelDao.GetSomething(id);
        return model;
    }
}

Related posts

Recent Viewed