简体   繁体   English

在MVC4中从我的控制器传递复杂数据类型时,在web api中获取null

[英]Getting null in web api when passing complex data type from my controler in MVC4

I have create a seprate web api project and another mvc4 project. 我创建了一个独立的web api项目和另一个mvc4项目。 When i am passing integer or string to web api its working fine and geting saved. 当我将整数或字符串传递给web api时,其工作正常并且保存了。 But now i trying to pass comlex data to web api but i getting null in my project. 但现在我尝试将comlex数据传递给web api,但我在项目中得到null。 My codes are. 我的代码是。

MVC4... 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.... Web Api ....

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

MVC4代码

WebApi代码

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

above statement is wrong this would resutl in "http://localhost:1900/api/LoginApi?SM=Somenamespace.StudentModel"; 上面的语句是错误的,这将在"http://localhost:1900/api/LoginApi?SM=Somenamespace.StudentModel"; as ToString method of Object returns fully qualified text of class name with namespace if any. As的ToString方法返回带有命名空间的类名的完全限定文本(如果有)。

instead of specifying it on query string you should post studentmodel data. 而不是在查询字符串上指定它,你应该发布studentmodel数据。

Here is some good SO thread on posting data to url with WebClient . 这是使用WebClient数据发布到url的一些好的SO线程。 link 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM