2

for 3 days i'm trying to send my object to my webapi. I just can't do it from c# :/

I was trying PostAsync method, i was trying to send json (but then I can't deserialize), i was trying to use JObject but can't do nothing with it. I just don't know how to do this.

I got rly simple method

    public string PostMethod(Location param)
    {
        var test = param;

        return test.name;
    }

how can I send Location object from c# so I won't get null in webapi ? Only thing i've achived is to send a simple type like 'string'. But none of my metrhods works with more complex objects.

Below Location object

public partial class Location
{
    public Location()
    {
        this.Category = new HashSet<Category>();
    }

    public int id { get; set; }
    public Nullable<System.DateTime> create_date { get; set; }
    public Nullable<System.DateTime> modify_date { get; set; }
    public Nullable<int> create_user_id { get; set; }
    public Nullable<int> modify_user_id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public Nullable<int> photo_id { get; set; }
    public string www { get; set; }
    public string count_data { get; set; }
    public string status { get; set; }
    public Nullable<double> latitude { get; set; }
    public Nullable<double> longitude { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string zip { get; set; }
    public string telephone { get; set; }
    public string street { get; set; }

    public virtual AzureMedia AzureMedia { get; set; }
    public virtual Users Users { get; set; }
    public virtual Users Users1 { get; set; }
    public virtual ICollection<Category> Category { get; set; }
}

1 Answer 1

4

Did you install the newest Web API from NuGet? And you need the Json.NET too for this.

This should do the trick:

    Location loc = new Location( ..... );

    var jsonFormatter = new JsonMediaTypeFormatter();

    HttpContent content = new ObjectContent<Location>(loc , jsonFormatter);

    HttpResponseMessage responseMessage = client.PostAsync(uri, content).Result;
Sign up to request clarification or add additional context in comments.

1 Comment

@Remy but param is in my webapi and I need to send it from client application. Thats why I'm asking what is param in my client application

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.