How to host a asp .net core application under a sub folder

ScottC picture ScottC · Oct 5, 2017 · Viewed 7.4k times · Source

I have a asp .net core app running on Linux using Kestrel.

It binds to the ip ok on port 80.

But the nginx reverse proxy site needs to host the app under a non-root path e.g.

http://somesite.com/myapp

So this is ok, the app loads, but the app does not know about the myapp path - so tries to load content path resources from root. Therefore css resources etc don't load.

How do I configure the app ideally at runtime to know about the url path.

Update:

I have found that in Startup.configure using app.UsePathBase("/myapp"); helps as the app will handle the request on this path OK - but the static file requests are then /myapp/images/example.jpg which return a 404.

The images folder is in wwwroot - the default as I understand it for UseStaticFiles.

I would have expected the request to understand that /myapp was in effect virtual.

Answer

ScottC picture ScottC · Oct 11, 2017

OK - so 2 things had to be done to get the app running under a "virtual directory":

Inside the startup configure method I had to set the basepath for the app AND specify a path for the static files.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
 ...
 app.UseStaticFiles("/myapp");
 app.UsePathBase("/myapp");
 ...
}

The app also runs from / as well - not sure if this is desirable but its not a big issue for me right now.