Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prioritize Web Api Controllers over IHttpHandler?

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 Web ApiControllers have priority over my single IHttpHandler implementing class? In IIS, I put my AttributeRouting first and then there are all the aspx ones but still the Web Api Controller is not getting processed first..no matter what I do (having them under the same project). I don't want to introduce a separate 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..

Edit 2: More specifically: If the uri doesn't have a list of filtered things [file,message,property ...] it is routed to Resource.aspx

so api/file, api/message, api/property would be handle from other .ashx files - otherwise the traffic goes to Resource.ashx...

As a result the requests that have api/endpoint1, api/endpoint2, api/endpoint3 will all go to Resource.aspx. The question is how to route api/endpoint3 to the API Controller described below. 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 {
    //if url doesn't contain [file,message,property ...] route to Resource.ashx
 }

//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;
    }
}
like image 387
Michail Michailidis Avatar asked May 29 '15 14:05

Michail Michailidis


People also ask

What is the base controller for all Web API controller to inherit from?

In ASP.NET MVC 5 and Web API 2, there were two different Controller base types. MVC controllers inherited from Controller ; Web API controllers inherited from ApiController .

Which is the base class for Web API controllers?

Pretty much all Web API 2 controllers inherit from ApiController but Controller is the base class in the new world. In MVC 6 the same Controller base class can be used whether you are writing a RESTful API or an MVC website.

What is the difference between controller and ControllerBase?

Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests. If the same controller must support views and web APIs, derive from Controller . The following table contains examples of methods in ControllerBase .


1 Answers

I've found two solutions to this problem. The first is to modify module that rewrites urls by inserting check if Web API routing system can handle request. The second is to add another module to application, that will direct requests to Web API Handler using HttpContext.RemapHandler().

Here's code:

First solution.

If your module looks like this:

public class MyModule: IHttpModule
{
    public void Dispose(){}

    public void Init(HttpApplication context)
    {
        context.BeginRequest += (object Sender, EventArgs e) =>
        {
            HttpContext httpContext = HttpContext.Current;
            string currentUrl = httpContext.Request.Url.LocalPath.ToLower();
            if (currentUrl.StartsWith("/api/endpoint0") ||
                currentUrl.StartsWith("/api/endpoint1") || 
                currentUrl.StartsWith("/api/endpoint2"))
            {
                httpContext.RewritePath("/api/resource.ashx");
            }
        };
    }
}

Then you need to change it like this:

public void Init(HttpApplication context)
{
    context.BeginRequest += (object Sender, EventArgs e) =>
    {
        HttpContext httpContext = HttpContext.Current;
        var httpRequestMessage = new HttpRequestMessage(
            new HttpMethod(httpContext.Request.HttpMethod),
            httpContext.Request.Url);
        IHttpRouteData httpRouteData = 
            GlobalConfiguration.Configuration.Routes.GetRouteData(httpRequestMessage);
        if (httpRouteData != null) //enough if WebApiConfig.Register is empty
            return;

        string currentUrl = httpContext.Request.Url.LocalPath.ToLower();
        if (currentUrl.StartsWith("/api/endpoint0") ||
            currentUrl.StartsWith("/api/endpoint1") ||
            currentUrl.StartsWith("/api/endpoint2"))
        {
            httpContext.RewritePath("/api/resource.ashx");
        }
    };
}

Second solution.

Module for remapping handlers:

public class RemappingModule: IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.PostResolveRequestCache += (src, args) =>
        {
            HttpContext httpContext = HttpContext.Current;
            string currentUrl = httpContext.Request.FilePath;
            if (!string.IsNullOrEmpty(httpContext.Request.QueryString.ToString()))
                currentUrl += "?" + httpContext.Request.QueryString;
            //checking if url was rewritten
            if (httpContext.Request.RawUrl != currentUrl) 
            {
                //getting original url
                string url = string.Format("{0}://{1}{2}",
                    httpContext.Request.Url.Scheme,
                    httpContext.Request.Url.Authority,
                    httpContext.Request.RawUrl);
                var httpRequestMessage = new HttpRequestMessage(
                    new HttpMethod(httpContext.Request.HttpMethod), url);
                //checking if Web API routing system can find route for specified url
                IHttpRouteData httpRouteData = 
                    GlobalConfiguration.Configuration.Routes.GetRouteData(httpRequestMessage);
                if (httpRouteData != null)
                {
                    //to be honest, I found out by experiments, that 
                    //context route data should be filled that way
                    var routeData = httpContext.Request.RequestContext.RouteData;
                    foreach (var value in httpRouteData.Values)
                        routeData.Values.Add(value.Key, value.Value);
                    //rewriting back url
                    httpContext.RewritePath(httpContext.Request.RawUrl);
                    //remapping to Web API handler
                    httpContext.RemapHandler(
                        new HttpControllerHandler(httpContext.Request.RequestContext.RouteData));
                }
            }
        };
    }
}

These solutions work when method WebApiConfig.Register is empty, but if there were routes with templates like "api/{controller}" then any path with two segments starting with "api" would pass the check, even if there're no controllers with specified name and your module can do something userfull for this path. In this case you can, for example, use method from this answer to check if controller exists.

Also Web API routing system will accept route even if found controller don't handle requests for current http method. You can use descendant of RouteFactoryAttribute and HttpMethodConstraint to avoid this.

UPD Tested on this controllers:

[RoutePrefix("api/endpoint1")]
public class DefaultController : ApiController
{
    [Route("{value:int}")]
    public string Get(int value)
    {
        return "TestController.Get: value=" + value;
    }
}

[RoutePrefix("api/endpoint2")]
public class Endpoint2Controller : ApiController
{
    [Route("segment/segment")]
    public string Post()
    {
        return "Endpoint2:Post";
    }
}
like image 156
Valyok26 Avatar answered Sep 30 '22 09:09

Valyok26