为什么我不能把Json送回去。它是加下划线的,在当前情况下不存在。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Results;
using System.Web.Mvc;
//class and namespace detail removed to keep this post short
public async Task<IHttpActionResult> Post(string url, StringContent data, Dictionary<string, string> headers)
{
using (var client = new HttpClient())
{
var response = await client.PostAsync(url, data);
var result = await response.Content.ReadAsStringAsync();
//the line below is the error********
return Json(new { HttpStatusCode = HttpStatusCode.OK });
}
}我也尝试过install-package System.Json,但这没有帮助。
我意识到可能还有其他错误,这段代码从一小时前就开始运行了,但是我不明白为什么Json没有被识别
这来自类库中(如果重要的话)。
发布于 2017-09-02 13:37:45
该方法是Web的ApiController的助手方法,应该在ApiController派生类中调用。
public class MyApiController : System.Web.Http.ApiController {
public async Task<IHttpActionResult> Post(string url, StringContent data, Dictionary<string, string> headers) {
using (var client = new HttpClient()) {
var response = await client.PostAsync(url, data);
var result = await response.Content.ReadAsStringAsync();
return Json(new { HttpStatusCode = HttpStatusCode.OK });
}
}
}MVC Controller派生类也是如此。
public class MyController : Controller {
public async Task<ActionResult> Post(string url, StringContent data, Dictionary<string, string> headers) {
using (var client = new HttpClient()) {
var response = await client.PostAsync(url, data);
var result = await response.Content.ReadAsStringAsync();
return Json(new { HttpStatusCode = HttpStatusCode.OK }, JsonRequestBehavior.AllowGet);
}
}
}https://stackoverflow.com/questions/46014199
复制相似问题