Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core routing prefix

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

like image 767
Timur Lemeshko Avatar asked Jun 06 '19 08:06

Timur Lemeshko


People also ask

What is route prefix?

Route prefixes are associated with routes by design in attribute routing. It is used to set a common prefix for an entire controller. If you read the release notes that introduced the feature you may get a better understanding of the subject.

What is attribute routing in ASP.NET Core?

With attribute-based routing, we can use C# attributes on our controller classes and on the methods internally in these classes. These attributes have metadata that tell ASP.NET Core when to call a specific controller. It is an alternative to convention-based routing.

How do I enable routing in .NET Core?

Inside the ConfigureRoute method, you can configure your routes; you can see that this method has to take a parameter of type IRouteBuilder. The goal of routing is to describe the rules that ASP.NET Core MVC will use to process an HTTP request and find a controller that can respond to that request.

How do you override the prefix route?

We can override the RoutePrefix on a method, using the tilde(~) sign. We can easily define parameterized templates in the attribute based routing. We can have parameters defined in-between the API request URLs.


Video Answer


3 Answers

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://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.usepathbaseextensions.usepathbase?view=aspnetcore-2.2

like image 197
zhimin Avatar answered Oct 19 '22 03:10

zhimin


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

like image 28
ElettroAle Avatar answered Oct 19 '22 03:10

ElettroAle


[Route("MyApplication")]
public class MyController : Controller
{
    [HttpGet]
    public async Task<IActionResult> Login(string returnUrl = null)
    {
        // Blah!
    }
}
like image 4
Optional Option Avatar answered Oct 19 '22 05:10

Optional Option