【问题标题】:ASP.NET Core 3.0 change default endpoint route with attribute routingASP.NET Core 3.0 使用属性路由更改默认端点路由
【发布时间】:2019-12-11 09:51:04
【问题描述】:

应用在 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}");
        });

【问题讨论】:

  • 尝试使用app.UseEndpoints(endpoints =&gt; { endpoints.MapControllers(); });
  • @picolino 我已经试过了,同样的。
  • 您的意思是在继续mycontroller 时要导航到mycontroller/login
  • 是的,没错

标签: c# asp.net-core routing asp.net-mvc-routing


【解决方案1】:

在检查用户是否已登录或未重定向到登录页面之后,我的 Index 操作方法顶部的简单重定向完成了工作。

[HttpGet]
        public async Task<IActionResult> Index()
        {
            if (true)// implement cookie policy
                return RedirectToAction("Login");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2020-05-31
    • 2017-07-08
    • 1970-01-01
    • 2020-03-23
    相关资源
    最近更新 更多