Question

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.

How do I make a complex parameter nullable in an web api GET request? Excluding it doesn't work like in a POST request.

Was it helpful?

Solution

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

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.

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.

Going back to Web.Api 20 you also can define your own constraints to handle your own complex types by inheriting from 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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top