MVC Core 5 Redirect from root controller to Area controller

Mohan Raju 40 Reputation points
2021-12-02T14:11:55.583+00:00

I have a login controller which is at root level and after successful login I want to redirect the user to a controller inside Area.

I tried below code but it didn't work.

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

it's redirecting to Home/Dashboard?area=StaffAug instead of StaffAug/Home/Dashboard

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2021-12-03T12:38:47.063+00:00

    The area configuration must come first. See the official docs.

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

    Or

     app.UseEndpoints(endpoints =>  
     {  
         endpoints.MapAreaControllerRoute(  
             name: "StaffAug",  
             areaName: "StaffAug",  
             pattern: "StaffAug/{controller=Home}/{action=Dashboard}"  
         );  
           
              endpoints.MapControllerRoute(  
             name: "default",  
             pattern: "{controller=Login}/{action=Index}"  
         );  
     });  
    

    Also, you did not include the Id route parameter which might cause you issues later.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2021-12-02T19:35:21.12+00:00

    The code you shared works perfectly on my end. My best guess is there is a problem with your area configuration. Is there anyway you can share example code that illustrates this issue so the community is not forced to guess?

    Areas for controllers with views

    The Startup.cs file with areas configured according to the doc above.

    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?}");  
    });  
    

    Controller with the area attribute.

    [Area("StaffAug")]  
    public class DashboardController : Controller  
    {  
        public IActionResult Index()  
        {  
            return View();  
        }  
    }  
    

    Caller

    public IActionResult Index()  
    {  
        return RedirectToAction("Index", "Dashboard", new { area = "StaffAug" });  
    }  
    
    0 comments No comments

  2. Mohan Raju 40 Reputation points
    2021-12-03T08:15:26.107+00:00

    My Startup.cs file is below.

    app.UseEndpoints(endpoints =>
                    {
                        endpoints.MapControllerRoute(
                            name: "default",
                            pattern: "{controller=Login}/{action=Index}"
                        );
    
                        endpoints.MapControllerRoute(
                           name: "areas",
                           pattern: "{area:exists}/{controller=Home}/{action=Index}"
                         );
    
                        endpoints.MapAreaControllerRoute(
                            name: "StaffAug",
                            areaName: "StaffAug",
                            pattern: "StaffAug/{controller=Home}/{action=Dashboard}"
                        );
                    });
    

    My controller code is below

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

    Caller code

     public class LoginController : Controller
        {
            private readonly VMSDBContext _context;
    
            public LoginController(VMSDBContext context)
            {
                _context = context;
            }
    
             [HttpPost]
            public IActionResult Index()
            {
    
                   return RedirectToAction("Dashboard", "Home", new { area = "StaffAug"});
    
              }          
         }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.