简体   繁体   English

如何重定向到MVC3中的其他区域?

[英]How do I redirect to an other area in MVC3?

I have 3 areas at the moment (more will come when I get this working): Login , User and Admin . 目前,我有3个领域(当我开始工作时,将会更多): LoginUserAdmin The Default route in Global.asax go to the index action of the index controller in the login area ( Login\\Index\\Index ) which works fine, this is how I mapped that: Global.asax的默认路由转到登录区域( Login\\Index\\Index )中的索引控制器的索引操作,该操作正常,这是我如何映射的方式:

routes.MapRoute(
    "Default", // Route name
    "{area}/{controller}/{action}/{id}", // URL with parameters
    new { area = "Login", controller = "Index", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
    new[] { typeof(AppName.Areas.Login.Controllers.IndexController).Namespace }
); 

each of the 3 areas is registered like this in the AreaRegistration files: 3个区域中的每一个都在AreaRegistration文件中这样注册:

context.MapRoute(
    "Admin_default",
    "{area}/{controller}/{action}/{id}",
    new { area = "Admin", controller = "Index", action = "Index", id = UrlParameter.Optional },
    new[] { typeof(AppName.Areas.Admin.Controllers.IndexController).Namespace }
);

The Login controllers index action takes the users login details, looks up the type of user and redirects them to the appropriate area...... at least it should do. Login controllers index操作获取用户的登录详细信息,查找用户的类型并将其重定向到适当的区域……至少应该这样做。 From what I can gather searching google, the correct way to redirect to a specific area is this: 从搜集到的谷歌搜索信息来看,重定向到特定区域的正确方法是:

return RedirectToAction("Action", "Controller", new { area = UserType });

which doesn't seem to work, it just redirects to Login\\Controller\\Action and I can't work out why. 这似乎不起作用,它只是重定向到Login\\Controller\\Action ,我不知道为什么。

In my attempts to work out what was happening I used this: 在尝试解决正在发生的事情时,我使用了以下方法:

var url = Url.Action("Action", "Controller", new { area = "Admin" });

which produced the same Login\\Controller\\Action url. 产生相同的Login\\Controller\\Action网址。

Any help fixing this would be greatly appreciated 任何解决此问题的帮助将不胜感激

In the routes parameter, you just set new { area = "OtherAreaName" }. 在routes参数中,您只需设置新的{area =“ OtherAreaName”}。

EDIT 编辑

Oh, I see you already tried that. 哦,我知道您已经尝试过了。 I suppose the name of that area is correctly referenced and correctly named in the class AreaRegistration class, like this? 我想该区域的名称在AreaRegation类中正确引用并正确命名,像这样吗?

public class AuthorAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Author";
        }
    }

    // ...
}

Could it also be that you need authentication and you are being redirected to the login page? 可能还需要身份验证并将您重定向到登录页面吗?

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

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