asp.net mvc 3区域和url路由配置

时间:2011-09-19 13:31:49

标签: asp.net asp.net-mvc-3 url-routing asp.net-mvc-areas

我在为asp.net mvc3应用程序创建ulr路由时遇到问题。

我的项目有这样的结构:

    • EmployeeReport
      • 控制器
        • 报告
        • 报告
          • 列表
          • ....
  • 控制器
    • 登录
  • Viwes
    • 登录
      • ...

EmployeeReportAreaRegistration.cs:

public class EmployeeReportAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "EmployeeReport";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        var routes = context.Routes;

        routes.MapRoute(null, "vykazy/vykazy-zamestnance", new { Area = "EmployeeReport", controller = "Report", action = "List" });

    }
}

public class EmployeeReportAreaRegistration : AreaRegistration { public override string AreaName { get { return "EmployeeReport"; } } public override void RegisterArea(AreaRegistrationContext context) { var routes = context.Routes; routes.MapRoute(null, "vykazy/vykazy-zamestnance", new { Area = "EmployeeReport", controller = "Report", action = "List" }); } }

Global.asax:

        routes.MapRoute(null, "prihlasit", new { controller = "Login", action = "Login" });

        routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Default", action = "Welcome", id = UrlParameter.Optional });

        routes.MapRoute(null, "prihlasit", new { controller = "Login", action = "Login" });

        routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Default", action = "Welcome", id = UrlParameter.Optional });

嗯,我错在哪里?

由于

2 个答案:

答案 0 :(得分:1)

修改后的答案:

直接添加到Context.Routes意味着它会丢失有关区域的任何信息。

使用AreaRegistration.MapRoute(覆盖以放入区域信息)。

context.MapRoute(...);

或者将该区域放在DataTokens参数中(而不是像在此处所做的默认参数)

context.Routes.MapRoute("", "url", new {...}, null, new {area = this.AreaName});

答案 1 :(得分:0)

您所在地区的文件夹结构应如下所示:

    • EmployeeReport
      • 控制器
      • 观看次数