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

后端 未结 3 818
北荒
北荒 2021-02-08 05:32

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-r

相关标签:
3条回答
  • 2021-02-08 06:03

    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();
        ...
    

    }

    0 讨论(0)
  • 2021-02-08 06:15

    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.

    0 讨论(0)
  • 2021-02-08 06:21

    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.

    0 讨论(0)
提交回复
热议问题