ASP.NET Core routing prefix

老子叫甜甜 提交于 2020-05-14 18:19:09

问题


I'm developing an ASP.NET Core application. My application hosted with NGinx on url http://somedomain.com/MyApplication.

I need all requests routed to prefix /MyApplication.

My problem with controllers actions responses redirects to somedomain.com, not to somedomain.com/MyApplication.

Is there any way to configure routes to use prefix /MyApplication?

UPD: for example

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

redirects to somedomain.com, but i need to somedomain.com/MyApplication


回答1:


you can use the PathBase middleware just before Mvc like this :

partial class Startup {

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

}

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




回答2:


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



回答3:


If you are using MVC, you can try to change the default route format. In Startup.cs replace the line

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

with this one:

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

Let me know if it's what you need




回答4:


In App_Start/RouteConfig.cs:

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



回答5:


Attribute routing for ASP.NET Core.In the below example shows

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

OR you use Attribute routing with Http[Verb] attributes. To be add the path in [HttpGet("Your Path")],In the case of [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();
        }


来源:https://stackoverflow.com/questions/56473486/asp-net-core-routing-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!