简体   繁体   English

登录后 MVC Core 5 将用户重定向到在区域中查看

[英]MVC Core 5 Redirect user to View in Area after login

I have a login page which is outside Area and after successful login I want to redirect the user to a View inside Area.我有一个区域外的登录页面,成功登录后,我想将用户重定向到区域内的视图。

I tried below code but it didn't work.我尝试了下面的代码,但没有奏效。

return RedirectToAction("Dashboard", "Home", new { Area = "StaffAug"});  
return RedirectToAction("Dashboard", "StaffAug/Home");

it's redirecting to Home/Dashboard?area=StaffAug instead of StaffAug/Home/Dashboard它重定向到 Home/Dashboard?area=StaffAug 而不是 StaffAug/Home/Dashboard

[Area("StaffAug")] 
public class HomeController : Controller 
{ 
 private readonly ILogger<HomeController> _logger; 
 public HomeController(ILogger<HomeController> logger) { _logger = logger; } 
 public IActionResult Dashboard() { return View(); } 
}

Be sure add area route to Startup.cs:确保将区域路由添加到 Startup.cs:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "MyArea",
        pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

Reference:参考:

Areas in ASP.NET Core ASP.NET 内核中的区域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM