ASP.NET Core路由前缀

Tim*_*hko 6 .net c# routing asp.net-core

我正在开发一个ASP.NET Core应用程序。我的应用程序在url上托管了NGinx http://somedomain.com/MyApplication

我需要将所有请求路由到prefix /MyApplication

我的控制器动作响应问题重定向到somedomain.com,而不是somedomain.com/MyApplication

有什么方法可以配置路由以使用前缀/MyApplication

UPD:例如

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> Login(string returnUrl = null)
        {
            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            ViewData["ReturnUrl"] = returnUrl;
            return View();
        }
Run Code Online (Sandbox Code Playgroud)

重定向到somedomain.com,但我需要somesome.com/MyApplication

zhi*_*min 16

您可以像这样PathBase之前使用中间件Mvc

partial class Startup {

    public void Configure(
        IApplicationBuilder app,
        IHostingEnvironment env
    ) {
        app.UsePathBase(new PathString("/MyApplication"));
        app.UseMvc();
    }

}
Run Code Online (Sandbox Code Playgroud)

with the PathBase middleware, no need to change any mvc code, it will automatically add to the request and response.

please refer to https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.usepathbaseextensions.usepathbase?view=aspnetcore-2.2


小智 5

如果您使用的是 MVC,您可以尝试更改默认路由格式。在Startup.cs更换线

app.UseMvc(routes => { routes.MapRoute(name: "default", template: "/{controller=Home}/{action=Index}/{id?}"); });
Run Code Online (Sandbox Code Playgroud)

有了这个:

app.UseMvc(routes => { routes.MapRoute(name: "default", template: "MyApplication/{controller=Home}/{action=Index}/{id?}"); });
Run Code Online (Sandbox Code Playgroud)

让我知道这是否是您需要的