为什么ASP .NET MVC 5中没有Request.Content?

时间:2019-02-07 15:49:02

标签: c# asp.net-mvc-5

如何访问内容数据?

我可以获取Request.ContentLength,ContentType,但不能获取内容。

我尝试在字符串参数之前添加[FromBody]属性,但无论如何它返回null。

    [HttpPost]
    public string CreateItemType([System.Web.Http.FromBody] string json) // still returns null
    {
       var Content = Request.Content; // Error: 'HttpRequestBase' doesn't have a definition for 'Content'
       if (IsAuthorized(Request) == false) return "Authorization Error"; ...

有什么方法可以访问发送的数据?

我的用于发送数据的应用程序:

        using (var client = new HttpClient())
        using (var request = new HttpRequestMessage())
        {
            request.Method = HttpMethod.Post;
            request.RequestUri = new Uri(Link);
            request.Content = new StringContent(json, Encoding.UTF8, "application/json");
            request.Headers.TryAddWithoutValidation("Authorization", ApiKey);
            client.Timeout = TimeSpan.FromSeconds(10);
            var MyResponse = await client.SendAsync(request);
            MyResponse.EnsureSuccessStatusCode();
            content = await MyResponse.Content.ReadAsStringAsync();
            return content;
        }

P.S:我可以将所有参数传递到请求链接中,但是我的JSON字符串太大。

0 个答案:

没有答案