asp net web api 2区域路由

时间:2016-01-26 12:44:41

标签: asp.net asp.net-mvc routing area

我有以下项目结构:

Areas
-- MasterData
----- Controllers
-------- ArticleController.cs
-- ProjectManagement
----- Controllers
-------- ProjectController.cs

我的MasterDataAreaRegistration.cs

    public override void RegisterArea(AreaRegistrationContext context)
    {
      context.Routes.MapHttpRoute(
          name: "API MasterData Default",
          routeTemplate: "api/MasterData/{controller}/{id}",
          defaults: new { id = RouteParameter.Optional }
      );

      context.MapRoute(
          "MasterData_default",
          "MasterData/{controller}/{id}",
          new { id = UrlParameter.Optional },
          namespaces: new[] { "iX.Service.Areas.MasterData.Controllers" }
      );
    }

我的ProjectManagementAreaRegistration.cs

    public override void RegisterArea(AreaRegistrationContext context)
    {
      context.Routes.MapHttpRoute(
          name: "API ProjectManagement Default",
          routeTemplate: "api/ProjectManagement/{controller}/{id}",
          defaults: new { id = RouteParameter.Optional }
      );

      context.MapRoute(
          "ProjectManagement_default",
          "ProjectManagement/{controller}/{id}",
          new { id = UrlParameter.Optional },
          namespaces: new[] { "iX.Service.Areas.ProjectManagement.Controllers" }
      );
    }

我不明白为什么两个区域都有两个控制器。

api/ProjectManagement/Project
api/MasterData/Project

ProjectController只能在ProjectManagement中使用。 我在哪里更改我的代码?为什么?

我的RoutConfig.cs

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

      routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
          namespaces: new[] { "iX.Service.Controllers" }
      );
    }

我的WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
  // Web API configuration and services

  // Web API routes
  config.MapHttpAttributeRoutes();

  /*config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{id}",
      defaults: new { id = RouteParameter.Optional }
  );*/
}

谢谢!

0 个答案:

没有答案