如何从自定义 Web 应用程序目录(而不仅仅是根目录)运行 .NET Core API 应用程序?
How to run a .NET Core API app from a custom web application directory, not just root?
我有一个 .NET Core 2.0 MVC 和 API 应用程序。该应用程序可从 Web 根目录访问,例如localhost:51672/, localhost:51672/Home, localhost:51672/api/.... 我想从不同的 Web 应用程序目录中选择 运行 , 例如 localhost:51672/custom, localhost:51672/custom/Home, localhost:51672/custom/api/...
我假设在 Kestrel 中或调用 dotnet 运行 时有一些选项,但无法找到它。不想为了这个工作而修改代码中的几个地方,更喜欢可配置或 运行time 选项,而不是必须在代码中的几个地方硬连接自定义根路径。
下面使用“自定义”根,swagger 除外
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FusionVM V1");
});
app.UseMvc( routes =>
{
routes.MapRoute(
name: "default",
template: "custom/{controller=Home}/{action=Index}/{id?}");
}
);
对于 web api,能够使用 Contoller 上的 Route 属性使其正常工作,但确实不希望在所有控制器中都指定根路径。在开发中,使用“/”,而在生产中可从自定义 Web 应用程序目录获得。
[Route("custom/api/[controller]")]
public class MyController : Controller
找到有用的东西。在 Startup.cs 中添加了以下...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UsePathBase("/custom");
app.UseStaticFiles("/custom");
...
这适用于 MVC 和 Web Api。对于 Swagger,必须修改其 json 端点..
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/custom/swagger/v1/swagger.json", "My API");
});
我有一个 .NET Core 2.0 MVC 和 API 应用程序。该应用程序可从 Web 根目录访问,例如localhost:51672/, localhost:51672/Home, localhost:51672/api/.... 我想从不同的 Web 应用程序目录中选择 运行 , 例如 localhost:51672/custom, localhost:51672/custom/Home, localhost:51672/custom/api/...
我假设在 Kestrel 中或调用 dotnet 运行 时有一些选项,但无法找到它。不想为了这个工作而修改代码中的几个地方,更喜欢可配置或 运行time 选项,而不是必须在代码中的几个地方硬连接自定义根路径。
下面使用“自定义”根,swagger 除外
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "FusionVM V1");
});
app.UseMvc( routes =>
{
routes.MapRoute(
name: "default",
template: "custom/{controller=Home}/{action=Index}/{id?}");
}
);
对于 web api,能够使用 Contoller 上的 Route 属性使其正常工作,但确实不希望在所有控制器中都指定根路径。在开发中,使用“/”,而在生产中可从自定义 Web 应用程序目录获得。
[Route("custom/api/[controller]")]
public class MyController : Controller
找到有用的东西。在 Startup.cs 中添加了以下...
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UsePathBase("/custom");
app.UseStaticFiles("/custom");
...
这适用于 MVC 和 Web Api。对于 Swagger,必须修改其 json 端点..
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/custom/swagger/v1/swagger.json", "My API");
});