代码之家  ›  专栏  ›  技术社区  ›  Diego

MVC Api控制器系列化参数

  •  0
  • Diego  · 技术社区  · 8 年前

    我正在使用 HttpClient() PostAsJsonAsync 带有一些参数的类的实例。

    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;
        }
    }
    

    我的API控制器方法是这样的。

    [ActionName("Method")]
    [HttpGet]
    public int Method([FromBody] ContactWF userwf)
    {
        return 10;
    }
    

    它很好用。。。

    HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl + "Contact/Method", contactWF);
    

    用这个

    string jsonData = JsonConvert.SerializeObject(contactWF);
    HttpResponseMessage response = client.PostAsJsonAsync("api/Contact/Method", jsonData).Result;
    

    Error:405 ...

    Json

    我的Json字符串如下所示。

    "{\"Contact_id\":0,\"Description\":null,\"ProcessState_id\":2,\"Type_id\":0,\"Object_id\":0,\"Parent_id\":null}"
    

    怎么了?

    2 回复  |  直到 8 年前
        1
  •  1
  •   Vitaliy Smolyakov    8 年前

    方法PostAsJsonAsync自己序列化参数对象,因此它序列化了json 一串

    如果出于某种原因需要序列化对象本身,则使用方法 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);
    
        2
  •  1
  •   Hung Quach    8 年前

    在api控制器中将动词更改为HttpPost

    [ActionName("Method")]
    [HttpPost]
    public int Method([FromBody] ContactWF userwf)
    {
        return 10;
    }
    

    使现代化

    PostAsJsonAsync

    HttpResponseMessage response = client.PostAsJsonAsync("api/Contact/Method", contactWF).Result;
    

    看一看来自microsoft的示例代码 https://docs.microsoft.com/en-us/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);