C# ASP.NET核心路由前缀

C# ASP.NET核心路由前缀,c#,.net,asp.net-core,routing,C#,.net,Asp.net Core,Routing,我正在开发一个ASP.NET核心应用程序。我的应用程序在urlNGinx上托管http://somedomain.com/MyApplication 我需要将所有请求路由到前缀/MyApplication 我的控制器操作响应问题重定向到somedomain.com,而不是somedomain.com/MyApplication 有没有办法将路由配置为使用前缀/MyApplication UPD:例如 [HttpGet] [AllowAnonymous]

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

我需要将所有请求路由到前缀
/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();
        }
[HttpGet]
[异名]
公共异步任务登录(字符串returnUrl=null)
{
//清除现有的外部cookie以确保干净的登录过程
等待HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
ViewData[“ReturnUrl”]=ReturnUrl;
返回视图();
}

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

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

app.UseMvc(routes => { routes.MapRoute(name: "default", template: "/{controller=Home}/{action=Index}/{id?}"); });
关于这一点:

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

请告诉我您是否需要它

您可以在
Mvc
之前使用
PathBase
中间件,如下所示:

partial class Startup {

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

}
使用
PathBase
中间件,无需更改任何mvc代码,它将自动添加到请求和响应中

请参阅

[路线(“我的申请”)]
公共类MyController:Controller
{
[HttpGet]
公共异步任务登录(字符串returnUrl=null)
{
//废话!
}
}

在App\u Start/RouteConfig.cs中:

routes.MapRoute(
    name: "default",
    url: "MyApplication/{controller}/{action}/{id}",
    defaults: new
    {
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional
    }
);

ASP.NET核心的属性路由。下面的示例显示

[Route("MyApplication")]
public class MyController : Controller
{
    //You can have multiple routes on an action
    [Route("")] /MyApplication
    [Route("/test")] you have move to the /MyApplication/test 
    [HttpGet]
    public async Task<IActionResult> Login(string returnUrl = null)
    {
        //Your Code Session
    }
}
[路由(“MyApplication”)]
公共类MyController:Controller
{
//一个动作上可以有多个路由
[路由(“”)/MyApplication
[路由(“/test”)]您必须移动到/MyApplication/test
[HttpGet]
公共异步任务登录(字符串returnUrl=null)
{
//您的代码会话
}
}
或者你用 具有Http[动词]属性的属性路由。 要在[HttpGet(“您的路径”)]中添加路径,如果是[HttpPost(“您的路径”)]

[HttpGet(“/MyApplication”)]
[异名]
公共异步任务登录(字符串returnUrl=null)
{
//清除现有的外部cookie以确保干净的登录过程
等待HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
ViewData[“ReturnUrl”]=ReturnUrl;
返回视图();
}

如何生成应该包含“/MyApplication”但不包含“/MyApplication”的URL?我不会将此逻辑放在ASPCore应用程序中。而是让nginx去掉前缀
/MyApplication
,并将重要的内容传递给应用程序。我会看看是否能找到nginx解决这个问题的方法。传入请求到/MyApplication的映射由Nginx完成,修改响应中的任何URL也是Nginx的责任。如果你让你的C#程序意识到Nginx正在修改传入的请求,那么你需要保持Nginx和你的应用程序同步,这会引发问题。
[Route("MyApplication")]
public class MyController : Controller
{
    //You can have multiple routes on an action
    [Route("")] /MyApplication
    [Route("/test")] you have move to the /MyApplication/test 
    [HttpGet]
    public async Task<IActionResult> Login(string returnUrl = null)
    {
        //Your Code Session
    }
}
[HttpGet("/MyApplication")]
        [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();
        }