简体   繁体   English

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

[英]How do I make my API action accept null complex types?

So this works perfect 所以这很完美

//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);
}

But this doesn't. 但这不是。 Not sure how to specify a null complex type. 不知道如何指定一个空的复杂类型。

//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);
}

I even tried this but ran into new problems with "Multiple actions were found that match the request" 我什至尝试了此操作,但是遇到了“发现多个符合要求的操作”的新问题

//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);
}

Here is what my complex class looks like 这是我的复杂课堂的样子

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

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

UPDATE I know that changing my method to a POST and using the body content works, but lets say that I'm stubborn and wish to accomplish this using GET. 更新我知道将方法更改为POST并使用正文内容是可行的,但是可以说我很固执,希望使用GET完成此操作。

How do I make a complex parameter nullable in an web api GET request? 如何在Web API GET请求中使复杂参数为空? Excluding it doesn't work like in a POST request. 排除它不像POST请求那样工作。

What I think you are looking for is how to define "Routing Rules" 我认为您正在寻找的是如何定义“路由规则”

WebApi 2.0 let's use an attribute called Route to define different "Resource URLs" with some rules(constraints) like length, format and default values WebApi 2.0让我们使用名为Route的属性来定义不同的“资源URL”,并使用一些规则(约束),例如长度,格式和默认值

For example: 例如:

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 };
    }

Where the parameter age is of type integer and optional . 其中参数ageinteger类型和optional类型。

Or like this using a default value: 或像这样使用默认值:

[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 };
}
    ...

Or being able to customize your routings based on your needs like : 或者能够根据您的需求自定义路由,例如:

[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 };
}
...

In the first version of the WebApi this had to be done in the WebApiConfig class however the options are not the same, I think the only real option on that version was the use of regular expressions. 在WebApi的第一个版本中,必须在WebApiConfig类中完成此操作,但是选项并不相同,我认为该版本上唯一真正的选项是使用正则表达式。

Going back to Web.Api 20 you also can define your own constraints to handle your own complex types by inheriting from IHttpRouteConstraint 回到Web.Api 20,您还可以通过继承IHttpRouteConstraint来定义自己的约束来处理自己的复杂类型。

There is additional information regarding the scenario you described on the WEB.Api documentation site, it is possible that you may need to implement a TypeConverter . 在WEB.Api文档站点上有关于您描述的方案的其他信息,可能可能需要实现TypeConverter

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM