ASP.NET Core 3.0 使用属性路由更改默认端点路由
ASP.NET Core 3.0 change default endpoint route with attribute routing
应用程序在 ASP.NET 核心上,我们最近迁移到了 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");
应用程序在 ASP.NET 核心上,我们最近迁移到了 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");