4
votes

I have a scenario where I want to Host an ASP.NET MVC Web API application, and a single-page Website Project on the same URL.

Essentially, host the website on http://localhost/, and the API on http://localhost/api/.

In IIS, this is as simple as setting up an Application sub folder, and pointing it to the files on disk. In OWIN, I am unsure how to achieve this scenario.

I have a console application that will serve as the Web Host, and it's currently hosting the WebAPI application using the methods found in this code: SelfHostWebApiWithOwinAndUnity

Can anyone help?

1

1 Answers

2
votes

If I'm understanding you correctly, you want to host some static html file(s) as well as the web api. This is fairly simple to do with OWIN. I'm going to assume you already have the nuget packages for Web API, so go ahead and grab the StaticFiles package. Then in Startup.Configuration, you can add the following code:

var options = new FileServerOptions();
options.EnableDefaultFiles = true;
options.DefaultFilesOptions.DefaultFileNames = new[] { "index.html" };
options.DefaultFilesOptions.FileSystem = new PhysicalFileSystem("Web");
options.DefaultFilesOptions.RequestPath = PathString.Empty;
app.UseFileServer(options);

This will serve files from the folder "Web". The default file that is served will be "index.html", and any other files in the Web folder can be accessed with their file name (ie. localhost/otherpage.html). It won't allow access to subfolders of Web.

Then simply make sure your WebAPI routes all start with "api/"