0

Background

I have a web api project which uses complex types for GET requests, here is an example of a controller method, and its associated complex type

[RoutePrefix("api")]
public class MyController : ApiController
{
    [Route("Something")]    
    public IHttpActionResult GetSomething([FromUri]RequestObject request)
    {
        // do something to get "data"
        return Ok(data);
    }
}

// elsewhere
public class RequestObject
{
    [Required]
    public string SomeValue{get;set;}
}

This works with a url such as http://domain.com/api/Something?SomeValue=foo.

I would like to use alias' for these parameters, for which I will do some complex stuff (once I have this working) but effectively I have defined an attribute AliasAttribute.

[AttributeUsage(AttributeTargets.Property,AllowMultiple=true)]
public class AliasAttribute : Attribute
{
    public string ParameterName { get; private set; }
    
    public AliasAttribute(string parameterName)
    {
        this.ParameterName = parameterName;
    }
}

Which I would like to adorn onto my request model like so:

// elsewhere
public class RequestObject
{
    [Required,Alias("val")]
    public string SomeValue{get;set;}
}

Allowing my url to shorten to http://domain.com/api/Something?val=foo. This is a contrived and simplified example, but hopefully demonstrates the background.

Problem

ModelBinding in web api has become very complex compared to Mvc model binding. I am getting twisted up between IModelBinder, IValueProvider, HttpParameterBinding et al.

I would like an example of where I should hook in to the model binding to allow me to write the value to my model from the querystring - note that I only use this aliasing behaviour when the route uses the FromUri attribute (see MyController.GetSomething above).

Community
  • 1
  • 1
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 2
    Geez, lots of hate for this question - I have *absolutely zero idea* what is wrong with this question - AFAICT, its well laid out, and asks a sensible enough question. – Jamiec Oct 03 '16 at 09:59

1 Answers1

-1

Question title: Support aliased arguments in get requests for web api. I think you are re-inventing a wheel here AliasAttribute , and have not given a really good reason why you don't want to use community ways of doing this.

I have done something similar with Newtonsoft.Json serializer. But if you want something ootb I'd have to google around.

public class RequestObject
{
   [Required]
   [JsonProperty("vla")]
   public string SomeValue{get;set;}
}

Example SO that uses it: .Net NewtonSoft Json Deserialize map to a different property name


Here is a more agnostic way to do it.

[DataContract]
public class RequestObject
{
   [DataMember(Name="val", IsRequired=true)]
   public string SomeValue{get;set;}
}
miken32
  • 42,008
  • 16
  • 111
  • 154
Nix
  • 57,072
  • 29
  • 149
  • 198
  • 1
    Thanks for the answer, but its not an answer to the question asked. Im using GET requests, so there is no body to put JSON data in. – Jamiec May 18 '15 at 13:18
  • 2
    FYI the tooltip over the downvote button says "This answer was not useful". Your help is appreciated (as I commented above) but really, is not useful (in solving my issue). This is still true with the edit. And funnily enough, I have been googling all morning - Just linking to google is borderline rude! – Jamiec May 18 '15 at 13:26