Is there a way to handle form post data in a Web Api controller?

前端 未结 5 1035
清歌不尽
清歌不尽 2020-12-03 01:13

In ASP.NET MVC , one can access the form post data:

var thisData = Request.Form[\"this.data\"];

Is it possible to achieve the

5条回答
  •  鱼传尺愫
    2020-12-03 01:59

    If you just have an HttpRequestMessage, you have a couple of options:

    By referencing System.Net.Http.Formatting, you can use the ReadAsFormDataAsync() extension method.

    var formData = await message.Content.ReadAsFormDataAsync();
    

    If you don't want to include that library, you can do the same thing manually by using the HttpUtility helper class in System.Web.

    public async Task ParseFormDataAsync(HttpRequestMessage message)
    {
        string formString = await message.Content.ReadAsStringAsync();
        var formData = HttpUtility.ParseQueryString(formString);
        return formData;
    }
    

提交回复
热议问题