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
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.
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.
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.
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.
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
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
[Route("MyApplication")]
public class MyController : Controller
{
[HttpGet]
public async Task<IActionResult> Login(string returnUrl = null)
{
// Blah!
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With