У меня в приложении два контроллера. Один - это контроллер api, другой - mvc. Мои маршруты выглядят так:
api: /api/account/login
mvc: /account/login
Мои контроллеры:
[ApiController]
[Route("api/[controller]")]
public class AccountController : ControllerBase
{
private readonly IMediator _mediator;
public AccountController(
IMediator mediator)
{
_mediator = mediator;
}
[HttpPost("login")]
public async Task<AuthenticationResultViewModel> Login(LoginRequest loginData, CancellationToken cancellationToken)
=> await _mediator.Send(loginData, cancellationToken);
}
// this one's in a different namespace
[Route("[controller]")]
public class AccountController : Controller
{
private readonly IMediator _mediator;
public AccountController(
IMediator mediator)
{
_mediator = mediator;
}
[HttpGet("login")]
public IActionResult Login([FromQuery] string r)
=> View();
[HttpPost("login")]
public async Task<IActionResult> Login([FromQuery] string r, [FromForm] LoginRequest login, CancellationToken cancellationToken)
{
await _mediator.Send(login, cancellationToken);
if (!string.IsNullOrWhiteSpace(r))
return Redirect(r);
return RedirectToAction("Index", "Home");
}
}
My ServiceCollection :
//...
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
services.AddControllersWithViews()
//...
Мой конвейер таков:
app.UseResponseCaching();
app.UseResponseCompression();
app.UseCors();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
// here i tried defining my routes
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
// I have tried the following without success
// I'm actually lost :'(
//endpoints.MapControllerRoute("mvc", "{controller}/{action}/{id?}", new { controller = "Home", action = "Index" });
//endpoints.MapControllerRoute("api", "api/{controller}/{action}/{id?}");
});
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.All,
RequireHeaderSymmetry = false
});
Теперь моя актуальная проблема:
Когда я использую UrlHelper, на мой взгляд, всегда возвращает маршрут api!
<form action="@Url.Action("Login", "Account")" method="post">
<div class="form-group">
<input asp-for="Username" type="text" placeholder="Username" />
</div>
<div class="form-group">
<input asp-for="Password" type="password" placeholder="Password"/>
</div>
<div class="form-group">
<button type="submit">Login</button>
</div>
</form>
@Url.Action("Login", "Account")
=> /api/account/login
- Почему это происходит?
- Как я могу вернуть UrlHelper мой MVC маршрут в моих представлениях?