为所有区域的Web API帮助页面翻炒动作 [英] Web API help page duplicating Action for all areas

查看:194
本文介绍了为所有区域的Web API帮助页面翻炒动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Web API 2个工作,而且似乎已经拉起我现有的API调用,除了它的复制为我有每个地区的所有呼叫。例如,假设我有3个区域,并在其中的一个我有一个API调用,看起来像:

 公开的IList<串GT; GetStringList(字符串ID)
    {
        //做的工作在这里...
        返回新的List<串GT; {A,B,C};
    }

如果我有3个地方,那么Web API帮助页面将显示:


  

GET区1 / API / MyAPIController / GetStringList / {ID}


  
  

GET区2 / API / MyAPIController / GetStringList / {ID}


  
  

GET AREA3 / API / MyAPIController / GetStringList / {ID}


和MyAPIController只有在'区域2'的存在。这是为什么出3次,我怎么能解决这个问题?如果有帮助,我对AREA2区登记的是:

 公共类Area2AreaRegistration:AreaRegistration
{
    公众覆盖字符串AREANAME
    {
        得到
        {
            返回区域2;
        }
    }    公共覆盖无效RegisterArea(AreaRegistrationContext上下文)
    {
        context.MapRoute(
            Area2_default
            区域2 / {控制器} / {行动} / {ID}
            新{行动=索引,ID = UrlParameter.Optional}
        );        context.Routes.MapHttpRoute(
    名称:Area2_ActionApi
    routeTemplate:区域2 / API / {控制器} / {行动} / {ID}
    默认:新{ID = RouteParameter.Optional}
);    }
}


解决方案

虽然不是你的问题的解决方案,可以使用属性的路线图的行动作为临时解决方法。

要启用的属性添加路由的 config.MapHttpAttributeRoutes(); 在WebApiConfig登记,这应该是App_Start文件夹中。

 公共静态无效的注册(HttpConfiguration配置)
{
    //属性的路由。
    config.MapHttpAttributeRoutes();    //公约为基础的路由。
    config.Routes.MapHttpRoute(
        名称:DefaultApi
        routeTemplate:API / {}控制器/(编号),
        默认:新{ID = RouteParameter.Optional}
    );
}

一旦启用了路由属性,可以指定在一个动作路线:

 公共类BooksController中:ApiController
{
    [路线(API /书)]
    公共IEnumerable的<图书> GetBooks(){...}
}

您可以在这里阅读更多。看看路线prefixes(如上图所示),并确保启用了如图所示的页面的开头属性的路由。

编辑:

在您的情况:

  [路线(AREA2 / API / MyAPIController / GetStringList /(编号))
公众的IList<串GT; GetStringList(字符串ID)
{
    //做的工作在这里...
    返回新的List<串GT; {A,B,C};
}

I'm working with Web API 2, and it seems to pull up my existing API calls already, except it's duplicating all the calls for each area that i have. For example, say i have 3 areas, and in one of those i have an API call that looks like:

public IList<string> GetStringList(string id)
    {
        //do work here...
        return new List<string>{"a","b","c"};
    }

if i have 3 areas, then the web api help page will show:

GET area1/api/MyAPIController/GetStringList/{id}

GET area2/api/MyAPIController/GetStringList/{id}

GET area3/api/MyAPIController/GetStringList/{id}

and the MyAPIController only exists in 'area2'. Why is this showing 3 times, and how can i fix it? If it helps, my area registration for area2 is:

public class Area2AreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Area2";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Area2_default",
            "Area2/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );

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

    }
}

解决方案

While not a solution to your problem, you can use attributes to map the routes for actions as a temporary workaround.

To enable attributes for routing add config.MapHttpAttributeRoutes(); to the registration in WebApiConfig, which should be within the App_Start folder.

public static void Register(HttpConfiguration config)
{
    // Attribute routing.
    config.MapHttpAttributeRoutes();

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

Once you've enabled attribute routing you can specify a route over an action:

public class BooksController : ApiController
{
    [Route("api/books")]
    public IEnumerable<Book> GetBooks() { ... }
}

You can read more here. Take a look at route prefixes(shown above) and make sure you enable routing with attributes as shown in the beginning of the page.

Edit:

In your case:

[Route("area2/api/MyAPIController/GetStringList/{id}")]
public IList<string> GetStringList(string id)
{
    //do work here...
    return new List<string>{"a","b","c"};
}

这篇关于为所有区域的Web API帮助页面翻炒动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆