编程语言
首页 > 编程语言> > c# – 从MVC区域重定向会将我重定向到顶级操作吗?

c# – 从MVC区域重定向会将我重定向到顶级操作吗?

作者:互联网

我有以下项目结构.我在测试区内有两个家庭和帐户控制器,在路线级项目有一个.现在从区域登录后我想重定向区域的主页索引但是它将我重定向到路由级别主页索引.

enter image description here

测试区域注册MapRoute

public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Test_default",
                "Test/{controller}/{action}/{id}",
                new { controller = "Account", action = "Login", id = UrlParameter.Optional },
                namespaces: new[] { "MVCAreaSample.Areas.Test.Controllers" }
            );
        }

基准级maproute

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional },
                namespaces: new[] { "MVCAreaSample.Controllers" }
            );

区域重定向操作方法

[HttpPost]
        public ActionResult Login(TestModel test)
        {
            var candidateContext = "LoginContext";


            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(2, candidateContext, DateTime.Now, DateTime.Now.AddMinutes(60), true, "Manjay");
            string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

            HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

            //Check required for code contracts.
            if (System.Web.HttpContext.Current != null)
            {
                System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);

                if (System.Web.HttpContext.Current.Session != null)
                    System.Web.HttpContext.Current.Session["LoginContext"] = candidateContext;
            }


            return RedirectToAction("Index", "Home");
        }

我尝试在路线中给出区域名称.它适用于这种情况.但是假设我在两个级别都有适当的身份验证逻辑

RedirectToAction(“Index”,“Home”,new {area =“Test”});

它会给我基础级别的登录页面.

解决方法:

您只需确定现有区域路径并将其作为重定向中的参数传递,如下所示:

var currentArea = RouteData.DataTokens["area"];
return RedirectToAction("Index", "Home", new { area = currentArea });

要返回某个级别,您只需为该区域指定一个空字符串:

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

标签:c,asp-net,asp-net-mvc,asp-net-mvc-4,asp-net-mvc-areas
来源: https://codeday.me/bug/20190628/1315692.html