我想從C#作爲參數傳遞JSON對象到一個MVC動作,但是當我收到它的價值變成了空傳遞JSON從C#
任何建議?
var url = "myurl";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var jsonContent = "{\"addProfileInfo\":{\"Address\" : \"Add Profile\", \"Address2\" : \"add2\", \"ArabicAddress1\" : \"\", \"ArabicAddress2\" : \"\", \"ArabicContactPerson\" : \"\", \"ArabicFirstName\" : \"\", \"ArabicMiddleName\" : \"\", \"ArabicLastName\" : \"\", \"BirthDate\" : \"1998-01-22T00:00:00\", \"CSO\" : 120, \"CompanyActivityId\" : 1, \"ContactPerson\" : \"\", \"Corporate\" : \"No\", \"FaxNumber\" : \"\", \"FirstName\" : \"Add Profile\", \"GeographicalRegionId\" : 58, \"HomeNumber\" : \"0235731789\", \"LastName\" : \"Gamal\", \"LoggedInUserID\" : 1976819, \"MiddleName\" : \"AddProfile\", \"MobileNumber\" : \"01111122829\", \"NationalID\" : null, \"NationalityId\" : 1, \"OccupationId\" : 8, \"OfficeNumber\" : \"\", \"PagerNumber\" : \"0235731739\", \"ParentID\" : 1989567, \"PassportID\" : null, \"Password\" : \"DLDovk65\", \"SendNewsByMail\" : false, \"StateID\" : null, \"StatusID\" : 1, \"StatusReasonID\" : 1, \"TitleId\" : 4, \"UCID\" : \"1007102885\", \"UserAccountManagerID\" : 299489, \"UserComments\" : \"\", \"UserGender\" : 1, \"UserName\" : \"AP.902651\", \"UserPreferredEmail\" : \"[email protected]\", \"ZipCode\" : \"\"},\"sourceName\":\"LDNCRM\"}";
streamWriter.Write(jsonContent);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
Console.WriteLine(responseText);
}
動作是否需要強類型參數?當它爲空時,通常意味着該類型不匹配。 – atbebtg 2013-02-26 17:22:35
是的,它需要一個強類型的參數,但我實現了我自己的ObjectFilter從ActionFilterAttribute繼承,我將json轉換爲使用json網絡庫的強類型對象ActionExecutingContext filterContext參數爲null的問題 – hazem 2013-02-26 17:30:46
@atbebtg和btw它正在從jquery ajax調用,但從C#它不我認爲我的問題與構建JSON對象 – hazem 2013-02-26 17:32:29