Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web Api Routing (IIS vs Self Hosted)

I have found a minor difference in the routing base classes in ASP.NET Web Api which has forced me to write a little helper class which will allow me to define my routes just once. Is there a reason for this? I'm assuming it was too big a change to the framework to make both RouteCollections derive from the same base class or implement the same interface (which would have made this class much simpler)

public static class RouteMapper
{
    private class Route
    {
        public string Name { get; set; }
        public string Template { get; set; }
        public object Defaults { get; set; }

        public Route(string name, string template, object defaults)
        {
            Name = name;
            Template = template;
            Defaults = defaults;
        }
    }

    private static List<Route> GetRoutes()
    {
        return new List<Route>
                   {
                       new Route(
                           "API Default",
                           "api/{controller}/{id}",
                           new {id = RouteParameter.Optional})
                   };
    }

    public static void AddHttpRoutes(this HttpRouteCollection routeCollection)
    {
        var routes = GetRoutes();
        routes.ForEach(route => routeCollection.MapHttpRoute(route.Name, route.Template, route.Defaults));
    }

    public static void AddHttpRoutes(this RouteCollection routeCollection)
    {
        var routes = GetRoutes();
        routes.ForEach(route => routeCollection.MapHttpRoute(route.Name, route.Template, route.Defaults));            
    }
}

What this allows me to do is to call a simple AddHttpRoutes method in both my Global.asax and my integration tests.

Integration Tests

        var configuration = new HttpSelfHostConfiguration("http://localhost:20000");
        configuration.Routes.AddHttpRoutes();

        _server = new HttpSelfHostServer(configuration);
        _server.OpenAsync().Wait();

Global.asax

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.AddHttpRoutes();

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

Is this a known issue and is it likely to be fixed in a later release of ASP.NET Web Api?

like image 871
Antony Scott Avatar asked Feb 23 '12 08:02

Antony Scott


People also ask

Does the ASP Net Web API ability to both self hosting and IIS?

ASP.NET Web API does not need to always be hosted in IIS. You can host them in a separate process by creating a host application. Such a self-hosting is typically done using a Console application or a Windows application.

Can we host ASP Net Web API without IIS?

ASP.NET Web API does not require IIS. You can self-host a web API in your own host process. New applications should use OWIN to self-host Web API. See Use OWIN to Self-Host ASP.NET Web API 2.

Can Web API be hosted in IIS?

Web API can be hosted under IIS, in the same way as a web application. You have learned to create a Web API in the previous section. As you have seen there, a Web API is created with ASP.NET MVC project by default.

How can I improve my Web API routing?

Routing inside Web API Just as you would route a controller inside ASP.NET MVC you can route API controllers in Web API applications, by adding custom routes to the route table. However instead of using the MapRoute method you should use the MapHttpRoute method.


1 Answers

Yes, the routing is slightly different due to the fact that ASP.NET already has routing but we couldn'd depend on it directly since that would prevent Self-host support. We're still looking at how things could make more sense.

like image 143
marcind Avatar answered Sep 28 '22 06:09

marcind