加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-web-api – 在Web API 2中使用属性路由时限制自动帮助页

发布时间:2020-12-16 07:24:48 所属栏目:asp.Net 来源:网络整理
导读:我目前正在使用Web API 2的属性路由( http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2)实现Web API.我还使用帮助页面模块,以便从XML注释( http://www.asp.net/web-api/overview/creating-web-apis/creatin
我目前正在使用Web API 2的属性路由( http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2)实现Web API.我还使用帮助页面模块,以便从XML注释( http://www.asp.net/web-api/overview/creating-web-apis/creating-api-help-pages)自动生成文档.

对于这个API,我提供了对可选返回格式扩展的支持,因此每个API方法都有一对定义的路由,如下所示:

[HttpGet]
[Route("Path/Foo")]
[Route("Path/Foo.{ext}")]
public HttpResponseMessage DoFoo()
{
    // Some API function.
}

这允许用户点击其中任何一个并获得结果:

www.example.com/api/Controller/Path/Foo 
www.example.com/api/Controller/Path/Foo.json
www.example.com/api/Controller/Path/Foo.xml

我的问题是,当帮助页面使用MapHttpAttributeRoutes()生成文档时,它会为每个方法选择两个路径.所以现在我看到帮助:

api/Controller/Foo
api/Controller/Foo.{ext}

但我只想看到:

api/Controller/Foo.{ext}

我宁愿在每个方法上隐藏非扩展路由,这样每个方法只显示一个帮助页面条目.

有没有其他人尝试类似的东西?有没有我失踪的工作?

解决方法

我的问题是,你的api的消费者会轻易搞清楚{ext}是可选的吗?…就个人而言,我更喜欢默认行为……但无论如何以下是我能想到的一些解决方法:

>快速而肮脏的解决方法.将DoFoo拆分为2个动作,如DoFoo()和DoFooWithExt.请注意,我使用的是名为ApiExplorerSettings的属性,用于HelpPage目的.示例如下:

[HttpGet]
[Route("Path/Foo")]
[ApiExplorerSettings(IgnoreApi=true)]
public HttpResponseMessage DoFoo()
{
    return DoFooHelper();
}

[HttpGet]
[Route("Path/Foo.{ext}")]
public HttpResponseMessage DoFooWithExt()
{
    return DoFooHelper();
}

private HttpResponseMessage DoFooHelper()
{
    //do something
}

>创建自定义ApiExplorer(HelpPage功能在内部使用)并检查特定路径,如下所示,并可决定是否显示该特定路径的操作.

// update the config with this custom implementation
config.Services.Replace(typeof(IApiExplorer),new CustomApiExplorer(config));

public class CustomApiExplorer : ApiExplorer
{
    public CustomApiExplorer(HttpConfiguration config) : base(config)
    {

    }

    public override bool ShouldExploreAction(string actionVariableValue,HttpActionDescriptor actionDescriptor,IHttpRoute route)
    {
        if (route.RouteTemplate.EndsWith("Path/Foo",StringComparison.OrdinalIgnoreCase))
        {
            return false;
        }

        return base.ShouldExploreAction(actionVariableValue,actionDescriptor,route);
    }
}

>从默认的ApiExplorer获取所有ApiDescription列表,然后过滤掉您不喜欢的描述.例:Configuration.Services.GetApiExplorer().ApiDescriptions.Where((apiDesc)=>!apiDesc.RelativePath.EndsWith(“Path / Foo”,StringComparison.OrdinalIgnoreCase))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读