如何通过ASP.NET Core MVC重定向到区域管理中的特定操作?

时间:2018-11-16 15:00:28

标签: routing asp.net-core-mvc

我有一个登录页面,我想将每个用户重定向到管理区域的特定操作。我找到了很多有关如何重定向到特定操作的链接。例如,我尝试了这些方法RedirectToPageRedirectToActionRedirectToRoute,但是所有这些方法都不适合我。

使用这种方法后,您可以看到我的问题所在。

 return RedirectToAction("Index", "Dashboard",new { area = "Admin"});

enter image description here

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null){
   ViewData["ReturnUrl"] = returnUrl;
   if (!ModelState.IsValid){
      return View(model);
   } else {
     var result = await _signInManager.PasswordSignInAsync(model.UserName,model.Password, model.RememberMe, lockoutOnFailure: false);
     var user = await _userManager.GetUserAsync(HttpContext.User);
     if (result.Succeeded){
        if (user != null){
           ApplicationUser appUser = new ApplicationUser(){
              Id = user.Id,
              Name = user.Name,
              UserName = user.UserName,
              Email = user.Email,
             LastName = user.LastName,
           };
           var userRole = await _userManager.GetRolesAsync(appUser);
           if (userRole != null){
              if (string.IsNullOrEmpty(returnUrl)){
                 if (_userManager.IsInRoleAsync(appUser, "مدیر کل").Result){
                    return RedirectToAction("Index", "Dashboard",new { area = "Admin"});
                    // return RedirectToPage("/Dashboard", new { area = "Admin" });
                    //return RedirectToPage("/Admin/Views/Dashboard/Index.cshtml");
                 }
              }
           }
        }
     }
   return View(model);
   }
}

app.UseMvc(routes =>{
    routes.MapRoute(
        name: "areas",
        template: "{area:exists}/{controller=Dashboard}/{action=Index}/{id?}"
    );
});

app.UseMvc(routes =>{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}"
    );
});

[Area("Admin")]
[Authorize(Roles = "مدیر")]
public class DashboardController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

0 个答案:

没有答案