[英]MVC Api Controller Serielized parameters
I am doing an MVC 5 Application, and I am calling a API controller method that is in another Solution. 我正在执行MVC 5应用程序,并且正在调用另一个解决方案中的API控制器方法。
I am using HttpClient()
. 我正在使用
HttpClient()
。 and I am calling PostAsJsonAsync
with some parameters, an instance of a class. 我用一些参数(一个类的实例)调用
PostAsJsonAsync
。
It looks like this. 看起来像这样。
string apiUrl = "localhost:8080/api/";
ContactWF contactWF = new contactWF();
contactWF.contact_id=0;
contactWF.UserOrigin_id=20006
contactWF.ProcessState_id=2;
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl + "Contact/Method", contactWF);
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<int>().Result;
}
}
My API controller method is like this. 我的API控制器方法就是这样。
[ActionName("Method")]
[HttpGet]
public int Method([FromBody] ContactWF userwf)
{
return 10;
}
It Works fine... 它工作正常...
My problem is when I try Serialized the parameter class instance I replace line 我的问题是当我尝试序列化参数类实例时,我替换了行
HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl + "Contact/Method", contactWF);
with this one 与这个
string jsonData = JsonConvert.SerializeObject(contactWF);
HttpResponseMessage response = client.PostAsJsonAsync("api/Contact/Method", jsonData).Result;
I've got an Error:405
... 我有一个
Error:405
...
It looks like the Json
string it is not recognize as a Parameter. 看起来像无法识别为参数的
Json
字符串。
My Json string looks like this. 我的Json字符串看起来像这样。
"{\"Contact_id\":0,\"Description\":null,\"ProcessState_id\":2,\"Type_id\":0,\"Object_id\":0,\"Parent_id\":null}"
that is ContactWD class converter to json. 那是ContactWD类到json的转换器。
What´s wrong? 怎么了?
Change verb to HttpPost in your api controller 在API控制器中将动词更改为HttpPost
[ActionName("Method")]
[HttpPost]
public int Method([FromBody] ContactWF userwf)
{
return 10;
}
Update 更新资料
You don't need to serialize object in PostAsJsonAsync
您不需要在
PostAsJsonAsync
序列化对象
HttpResponseMessage response = client.PostAsJsonAsync("api/Contact/Method", contactWF).Result;
Take a look at sample code from microsoft https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/testing 查看来自Microsoft https://docs.microsoft.com/zh-cn/aspnet/core/mvc/controllers/testing的示例代码
internal class NewIdeaDto
{
public NewIdeaDto(string name, string description, int sessionId)
{
Name = name;
Description = description;
SessionId = sessionId;
}
public string Name { get; set; }
public string Description { get; set; }
public int SessionId { get; set; }
}
//Arrange
var newIdea = new NewIdeaDto("Name", "", 1);
// Act
var response = await _client.PostAsJsonAsync("/api/ideas/create", newIdea);
// Assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
Method PostAsJsonAsync serialize parameter object himself, so it serialized your json string again. 方法PostAsJsonAsync自己序列化参数对象,因此它再次序列化了json 字符串 。
If you need serialize object himself for some reason, then use method HttpClient.PostAsync
如果出于某种原因需要自己序列化对象,请使用方法
HttpClient.PostAsync
string jsonData = JsonConvert.SerializeObject(contactWF);
var stringContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("api/Filler/CountMensajeByUser", stringContent);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.