niklodeon
niklodeon

Reputation: 1380

Accessing views between different areas

I have two different Areas (User & Report) and they have two views (Login & Index) respectively.

What I am trying to do is on successful login user should be redirected to Report/Index page but its not working.

Its always inside User area only. Can some one point out what needs to be corrected here. Thanks!!! Help will be appreciated...

enter image description here

public class UserController : Controller
    {    
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(Models.User user)
        {
            if (ModelState.IsValid)
            {
                if (user.IsValid(user.UserName, user.Password))
                {
                    return RedirectToAction("Report/Report/Index");
                }
                else
                {
                    ModelState.AddModelError("", "Login data is incorrect!");
                }
            }
            return View(user);
        }
      }

public class ReportController : Controller
{
    // GET: Report/Report
    public ActionResult Index()
    {
        return View();
    }
}

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "User", action = "Login", id = UrlParameter.Optional },
                namespaces: new[] { "DWP_MVC.Areas.User.Controllers" }
            ).DataTokens.Add("area", "User");

            routes.MapRoute(
                name: "Report",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Report", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "DWP_MVC.Areas.Report.Controllers" }
            ).DataTokens.Add("area", "Report");
        }
    }

public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "User_default",
                "User/{controller}/{action}/{id}",
                new { action = "Login", id = UrlParameter.Optional }
            );
        }

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Report_default",
        "Report/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}

Upvotes: 1

Views: 232

Answers (1)

Your RedirectToAction() call must include the area name if you are using areas in your MVC structure.

In your example the following code would work:

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

As an aside, if you wish to redirect from one area to a controller/view which is not within the Area folder, you can utilize area = ""

Upvotes: 1

Related Questions