【问题标题】:ASP.NET Core routing prefixASP.NET Core 路由前缀
【发布时间】:2019-10-21 17:33:06
【问题描述】:

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

我需要将所有请求路由到前缀 /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();
        }

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

【问题讨论】:

  • 如何生成应该包含“/MyApplication”但不包含的 URL?
  • 我不会把这个逻辑放在 ASPCore 应用程序中。而是让 nginx 去掉前缀 /MyApplication 并将重要的内容传递给应用程序。
  • 我会看看我是否能找到解决这个问题的 Nginx 方法。传入请求到 /MyApplication 的映射由 Nginx 完成,修改响应中的任何 URL 也应该是 Nginx 的责任。如果你让你的 C# 程序知道 Nginx 正在修改传入的请求,那么你需要让 Nginx 和你的应用程序保持同步,这就是在问问题。

标签: c# .net asp.net-core routing


【解决方案1】:

ASP.NET Core 的属性路由。在下面的示例中显示

[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
    }
}

或者你使用 带有 Http[Verb] 属性的属性路由。 在[HttpGet("Your Path")]中添加路径,在[HttpPost("Your Path")]的情况下。

[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();
        }

【讨论】:

    【解决方案2】:

    在 App_Start/RouteConfig.cs 中:

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

    【讨论】:

      【解决方案3】:
      [Route("MyApplication")]
      public class MyController : Controller
      {
          [HttpGet]
          public async Task<IActionResult> Login(string returnUrl = null)
          {
              // Blah!
          }
      }
      

      【讨论】:

        【解决方案4】:

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

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

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

        请参考https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.usepathbaseextensions.usepathbase?view=aspnetcore-2.2

        【讨论】:

        • 这在 asp.net core 6 中似乎没有任何作用......很奇怪。
        • @ReiMiyasaka 不,从 2.1 到 6.0 都支持。 docs.microsoft.com/en-us/dotnet/api/…>
        • 原来这是 6 中的一个错误。解决方法是显式调用app.UseRouting()。 github.com/dotnet/aspnetcore/issues/38448
        【解决方案5】:

        如果您使用的是 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?}"); });
        

        如果你需要,请告诉我

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-11-30
          • 2020-10-07
          • 1970-01-01
          • 1970-01-01
          • 2016-06-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多