0

I have create a seprate web api project and another mvc4 project. When i am passing integer or string to web api its working fine and geting saved. But now i trying to pass comlex data to web api but i getting null in my project. My codes are.

MVC4...

readonly string uri = "http://localhost:1900/api/LoginApi";
public string RegisterIcaUser(StudentModel _StudentModel)
    {
        using (WebClient webClient = new WebClient())
        {
            string newUri = uri + "?SM=" + _StudentModel;
            return JsonConvert.DeserializeObject<string>(webClient.DownloadString(newUri));
        }          
    }

Web Api....

 [HttpGet]
    public string RegisterIcaUser([FromUri]StudentModel SM)
    {
        string strResult = "F";
        return strResult;
    } 

MVC4 Code

WebApi Code

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Raghubar
  • 2,768
  • 1
  • 21
  • 31

1 Answers1

0
string newUri = uri + "?SM=" + _StudentModel;

above statement is wrong this would resutl in "http://localhost:1900/api/LoginApi?SM=Somenamespace.StudentModel"; as ToString method of Object returns fully qualified text of class name with namespace if any.

instead of specifying it on query string you should post studentmodel data.

Here is some good SO thread on posting data to url with WebClient. link

Community
  • 1
  • 1
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
  • How would i pass the studentmodel? The link you have given has example of passing string value. – Raghubar Feb 23 '15 at 05:57
  • @Raghubar You need to specify each property of the student model like this `string myParameters = "param1=value1&param2=value2&param3=value3";` – Jenish Rabadiya Feb 23 '15 at 06:04
  • @Raghubar another alernative to post values using C# is `PostAsJsonAsync` for detail visit this link. => http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client – Jenish Rabadiya Feb 23 '15 at 06:06