2

The get request is from an SMS API delivery report to get the information about the SMS.

One of variable that will be posted to my api is this: ?err-code=0. Is it possible to do it in a .Net Web API solution or should I use another language?

Web API Get Method:

    public HttpResponseMessage Get([FromUri]TestModel testingDetials)
    {           

        return Request.CreateResponse(System.Net.HttpStatusCode.OK);
    }

Model

  public class TestModel
  {
      public string foo { get; set; }

      public string err_code { get;set; }
  }

I tried various solution found on this website none of them work like adding [JsonProperty] and [DataMember] to the err_code property.

5
  • 1
    So you set [JsonProperty(PropertyName = "err-code")] ? And its Web API version 2? Commented Nov 1, 2015 at 17:18
  • Yes that what I did and also its Web API version 2. Do I need to add some setting or additional code to make it work? Commented Nov 1, 2015 at 17:55
  • I would have expected it to work. foo does bind? You could do a custom model binder? Commented Nov 1, 2015 at 19:16
  • I re-tried using JsonProperty and found out that it works if you pass it as JSON not from a url. I searched again and found that by adding this line of code works for my case HttpContext.Current.Request["err-code"]. Thank you for your help and time. Commented Nov 1, 2015 at 20:33
  • Oh. Yes. Sorry I assumed you were posting JSON. Commented Nov 2, 2015 at 2:18

2 Answers 2

3

You can use [JsonProperty(PropertyName = "err-code")] provided the request is being received as JSON. This is because JsonProperty is part of the Newtonsoft JSON serializer library which is what Web API uses to deserialize JSON. If the request is not JSON, the library is not used in the pipeline.

As you mentioned you can use HttpContext. If I remember correctly the model binding in MVC converts '-' to '_' but I could be wrong. Regardless to continue using strongly typed models, which I recommend, is to use model binding. This is basically writing a custom mapping between the http context and the model. You could even expand the usual one and map something like "err-code" to a property called ErrCode automatically by writing a convention based one. Here is an example, scroll a bit: http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api Happy Coding! (Through I would provide a complete answer for the sake of...well... having a complete answer)

Sign up to request clarification or add additional context in comments.

Comments

1

For my case I created a model binder to convert the var "_" to "-" and setting the value by using reflection. This answer is just for a reference. Here is the code: (This solution is used for Web API not MVC)

public class SmsReceiptModelBinder : IModelBinder
{

    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(SmsReceiptModel))
        {
            return false;
        }


        Type t = typeof(SmsReceiptModel);

        var smsDetails = new SmsReceiptModel();
        foreach (var prop in t.GetProperties())
        {
            string propName = prop.Name.Replace('_', '-');
            var currVal = bindingContext.ValueProvider.GetValue(
                     propName);
            if (currVal != null)
                prop.SetValue(smsDetails, Convert.ChangeType(currVal.RawValue, prop.PropertyType), null);
        }

        bindingContext.Model = smsDetails;
        return true;

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.