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

↘锁芯ラ 提交于 2021-02-06 10:29:49

问题


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.


回答1:


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.




回答2:


For me (using .NET Core 3.1) it was a bit different, using ScottC's answer it failed to find the static files as it was looking in the root of the my-app directory, not in my-app/wwwroot

This config worked for me in Startup.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    if (env.IsDevelopment())
    {
        ...
    }
    else
    {
        ...
        app.UsePathBase("/my-app");
    }

    app.UseStaticFiles();
    ...

}




回答3:


ScottC,

Once your web content is under wwwroot you have to add app.UseStaticFiles() on the configure section on Startup class.

See the example below:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();
}

Hope to have helped you.



来源:https://stackoverflow.com/questions/46593999/how-to-host-a-asp-net-core-application-under-a-sub-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!