当前位置: 移动技术网 > IT编程>开发语言>.net > Web API 约定

Web API 约定

2020年04月29日  | 移动技术网IT编程  | 我要评论

御泥坊面膜怎么样,怎么缩阴,义乌人才网最新招聘信息

web api 约定

默认的约定

集将应用于 contactsconventioncontroller 中的所有操作:

[apicontroller]
[apiconventiontype(typeof(defaultapiconventions))]
[route("api/[controller]")]
public class contactsconventioncontroller : controllerbase
{

默认的约定集将应用于程序集中的所有操作:

[assembly: apiconventiontype(typeof(defaultapiconventions))]
namespace apiconventions
{
    public class startup
    {

创建 web api 约定

如果默认 api 约定不能满足需要,请创建自己的约定。 约定是:

  • 带有方法的静态类型。
  • 能够对操作定义和。

响应类型

这些方法使用 [producesresponsetype][producesdefaultresponsetype] 属性进行批注。 例如:

c#复制

public static class myappconventions
{
    [producesresponsetype(statuscodes.status200ok)]
    [producesresponsetype(statuscodes.status404notfound)]
    public static void find(int id)
    {
    }
}

如果没有更具体的元数据属性,则将此约定应用于程序集可强制实现以下内容:

  • 该约定方法应用于所有名为 find 的操作。
  • id 操作上存在名为 find 的参数。

命名要求

[apiconventionnamematch][apiconventiontypematch] 属性可应用于约定方法,确定它们所要应用的操作。 例如:

c#复制

[producesresponsetype(statuscodes.status200ok)]
[producesresponsetype(statuscodes.status404notfound)]
[apiconventionnamematch(apiconventionnamematchbehavior.prefix)]
public static void find(
    [apiconventionnamematch(apiconventionnamematchbehavior.suffix)]
    int id)
{ }

在上面的示例中:

  • 应用于该方法的 microsoft.aspnetcore.mvc.apiexplorer.apiconventionnamematchbehavior.prefix 选项表示该约定可匹配前缀是“find”的任何操作。 匹配的操作可以是 findfindpetfindbyid
  • 应用于该参数的 microsoft.aspnetcore.mvc.apiexplorer.apiconventionnamematchbehavior.suffix 表示该约定可匹配带有唯一以标识符作为后缀结尾的参数的方法。 示例包括 idpetid 等参数。 与此类似,可将 apiconventiontypematch 应用于类型,以约束参数类型。 params[] 参数指示无需显式匹配的剩余参数。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网