WebApi路由在区域中不起作用

时间:2012-11-21 06:59:58

标签: asp.net asp.net-web-api asp.net-mvc-routing asp.net-mvc-areas

我有一个应用程序,我已将管理面板移动到区域,从那时起我无法点击adminpanel api调用。这是主api注册和管理面板api注册的代码。

    //Api registration
    public static void Register(HttpConfiguration config)
    {

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


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


    //Area Registration
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_panel",
            "AdminPanel/{controller}/{action}/{id}",
            new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
        );


        context.Routes.MapHttpRoute(
          "Admin_Secondary",
          "AdminPanel/api/{controller}/{id}",
          new { id = RouteParameter.Optional }
      );
    }

我试图用URL localhost / taw / adminpanel / api / SearchAPI来点击它但它不起作用。我的路由有什么问题?

1 个答案:

答案 0 :(得分:7)

我发现了问题,路由的优先级产生了问题,我想在发布问题之前我应该​​asked the duck

      context.Routes.MapHttpRoute(
       "Admin_Secondary",
       "AdminPanel/api/{controller}/{id}",
       new { id = RouteParameter.Optional }
      );

必须来

    context.MapRoute(
        "Admin_panel",
        "AdminPanel/{controller}/{action}/{id}",
        new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
    );