[英]ASP.NET Core 3.0 change default endpoint route with attribute routing
应用程序在 ASP.NET Core 上,我们最近迁移到 3.0。
我需要导航到mycontroller/login ,因此登录操作而不是默认索引操作,但很难更改默认控制器操作方法。 我一直在获取我的索引页面,只有当我手动导航到 /login 时,我才会获取该页面。 同样作为约定,我需要保留属性路由。
[Route("/mycontroller")]
public class MyController : Controller
{
private readonly IAuthenticationService _authenticationService;
public MyController(IAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}
[Route("login")]
[HttpPost("login")]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
try
{
var user = await _authenticationService.PerformCheckAndLogin(model.UserName, model.Password);
if (user != null)
{
var userClaims = new List<Claim>
在我的配置方法中,我尝试过:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller=mycontroller}/{action=login}");
});
在检查用户是否登录或未重定向到登录页面后,我的 Index 操作方法顶部的简单重定向完成了工作。
[HttpGet]
public async Task<IActionResult> Index()
{
if (true)// implement cookie policy
return RedirectToAction("Login");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.