如何在子文件夹下托管ASP .NET Core应用程序 [英] How to host a asp .net core application under a sub folder

查看:648
本文介绍了如何在子文件夹下托管ASP .NET Core应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Kestrel在Linux上运行的asp .net核心应用程序.

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

它绑定到端口80上的ip ok.

It binds to the ip ok on port 80.

但是nginx反向代理站点需要在非根路径下托管应用程序,例如

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

http://somesite.com/myapp

这样就可以了,应用程序加载了,但是应用程序不知道myapp路径-因此尝试从根目录加载内容路径资源.因此,css资源等无法加载.

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.

如何理想地在运行时配置应用程序以了解URL路径.

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

更新:

我发现在Startup.configure中使用app.UsePathBase("/myapp");该应用程序将在此路径上处理请求,因此可以提供帮助-静态文件请求随后为/myapp/images/example.jpg,返回404.

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.

images文件夹位于wwwroot中,据我所知,它是UseStaticFiles的默认文件夹.

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

我希望该请求能够理解/myapp实际上是虚拟的.

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

推荐答案

确定-要使应用程序在虚拟目录"下运行,必须做两件事:

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

在启动configure方法中,我必须设置应用程序的基本路径并指定静态文件的路径.

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.

这篇关于如何在子文件夹下托管ASP .NET Core应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆