pomber
pomber

Reputation: 23980

WebAPI routing - Map all requests to a sub-folder

I'm trying to do something very simple, but I couldn't find anything on google nor in the documentation.

I have an empty Web API application, with the default WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

The only thing I added is a folder with a html/js application:

WebApplication
|-- App_Start
|-- Controllers
|-- MySubFolder
|   |-- index.html
|   |-- js
|   |   `-- app.js
|   |-- css
|   |   `-- style.css
`-- Global.asax

I want every request that doesn't starts with "api/" to be redirected to MySubFolder. For example:

Upvotes: 2

Views: 2028

Answers (1)

pomber
pomber

Reputation: 23980

I've solved it using owin:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var httpConfiguration = new HttpConfiguration();

        // Configure Web API Routes:
        // - Enable Attribute Mapping
        // - Enable Default routes at /api.
        httpConfiguration.MapHttpAttributeRoutes();
        httpConfiguration.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        app.UseWebApi(httpConfiguration);

        // Make ./MySubFolder the default root of the static files in our Web Application.
        app.UseFileServer(new FileServerOptions
        {
            RequestPath = new PathString(string.Empty),
            FileSystem = new PhysicalFileSystem("./MySubFolder"),
            EnableDirectoryBrowsing = true,
        });

        app.UseStageMarker(PipelineStage.MapHandler);
    }
}

Upvotes: 1

Related Questions