c# - 如何使我的 API 操作接受 null 复杂类型?

标签 c# asp.net-web-api asp.net-web-api-routing

所以这很完美

//localhost:1234/api/task/getData?id=1&name=test&phone=123456

public object GetData(long id, [FromUri] User complexType)
{
    //complexType is binding correctly
    return runSomeCode(id, complexType ?? User.Current);
}

但这不是。不确定如何指定 null 复杂类型。

//localhost:1234/api/task/getData?id=1

public object GetData(long id, [FromUri] User complexType)
{
    //complexType is not null, but all class properties are
    return runSomeCode(id, complexType ?? User.Current);
}

我什至尝试过这个,但遇到了新问题“发现多个符合请求的操作”

//localhost:1234/api/task/getData?id=1

public object GetData(long id)
{
    return runSomeCode(id, User.Current);
}

public object GetData(long id, [FromUri] User complexType)
{
    return runSomeCode(id, complexType);
}

这是我的复杂类的样子

public class User
{
    public string Name {get;set;}
    public string Phone {get;set;}

    public static User Current
    {
        get
        {
            //code to get the current user
        }
    }
}

更新 我知道将我的方法更改为 POST 并使用正文内容是可行的,但可以说我很固执并希望使用 GET 来完成此操作。

如何使 Web API GET 请求中的复杂参数可为空?排除它不像在 POST 请求中那样工作。

最佳答案

我想你正在寻找的是如何定义“路由规则”

WebApi 2.0 让我们使用一个名为 Route 的属性来定义不同的“资源 URL”,其中包含一些规则(约束),如长度、格式和默认值

例如:

public class SomeController : ApiController{
     //where author is a letter(a-Z) with a minimum of 5 character and 10 max.      
    [Route("html/{id}/{newAuthor:alpha:length(5,10)}/age:int?")]
    public Book Get(int id, string newAuthor , int age){
        return new Book() { Title = "SQL Server 2012 id= " + id, Author = "Adrian & " + newAuthor };
    }

参数 age 的类型为 integeroptional

或者像这样使用默认值:

[Route("html/{id}/{newAuthor:alpha:length(5,10)}/age:int=33")]
public Book Get(int id, string newAuthor , int age){
  return new Book() { Title = "SQL Server 2012 id= " + id, Author = "Adrian & " + newAuthor };
}
    ...

或者能够根据您的需要自定义路由,例如:

[Route("url1/{id}")]
public Book Get(int id){
   return new Book() { Title = "SQL Server 2012 id= " + id, Author = "Adrian " };
}

[Route("url1/{id}/{author}")]
public Book Get(int id, string author){
   return new Book() { Title = "SQL Server 2012 id= " + id, Author = "Adrian & " + author };
}
...

在 WebApi 的第一个版本中,这必须在 WebApiConfig 类中完成,但是选项不一样,我认为该版本唯一真正的选项是使用正则表达式。

回到 Web.Api 20,您还可以定义自己的约束,通过继承 IHttpRouteConstraint

来处理自己的复杂类型

有关您在 WEB.Api 文档站点上描述的场景的其他信息,您可能需要实现 TypeConverter

http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

关于c# - 如何使我的 API 操作接受 null 复杂类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369507/

相关文章:

c# - ItemTemplate 中的控件无法在代码隐藏中调用

c# - HttpClient 调用 Windows 身份验证 Api Controller 方法...但没有 WindowsIdentity 出现

c# - Web API Restful 服务 ReadAsAsync<type> 不返回列表

c# - 我可以在 ASP.Net Web API Controller 中使用多个 Get 方法吗

c# - ASP WebAPI 可以有可选的 RoutePrefix 吗?

c# - Web API 2 中的 Http RouteUrl 和属性路由

c# - Web API 2.2,具有路由覆盖的继承 Controller (这可能吗)?

c# - WPF:通过自定义依赖属性绑定(bind)

c# - 如何在窗口关闭按钮单击时终止 wpf 应用程序中的所有线程

c# - 在 Window 服务中处理 WebApp.Start 实例的正确位置?