访问不同区域之间的视图

时间:2015-08-29 16:41:10

标签: asp.net-mvc model-view-controller asp.net-mvc-5

我有两个不同的区域(用户和报告),他们分别有两个视图(登录和索引)。

我想要做的是成功登录用户应该被重定向到报告/索引页面,但它不起作用。

它始终只在用户区内。有人可以指出这里需要纠正的问题。谢谢!!!帮助将不胜感激......

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

1 个答案:

答案 0 :(得分:1)

如果您使用的是MVC结构中的区域,则SIGCONT来电必须包含区域名称。

在您的示例中,以下代码可以使用:

RedirectToAction()

另外,如果您希望从一个区域重定向到不在Area文件夹中的控制器/视图,您可以使用return RedirectToAction("Index", "Report", new { area = "Report" });