如何从httpContext c#dotnet获取内容

时间:2017-09-20 09:30:41

标签: asp.net post .net-core

我正在尝试将帖子请求发送到WebApi,但是在post方法中找不到httpContent,尽管它在获取请求中很好

在WebApp中

class InvoiceUpdate(LoginRequiredMixin, generic.UpdateView):
    login_url = '/accounts/login'
    redirect_field_name = 'redirect_to'

    success_url = '/'

    template_name = 'invoice/invoice_update.html'

    model = Invoice

    fields = [
        'number',
        'price',
        'quantity',
        'comment',
        'notes',
    ]

这里是WebApi中的Post Request(我也希望绑定模型)

var json = JsonConvert.SerializeObject(new TestClass() { Id="someId"});
var content = new StringContent(json, Encoding.UTF8, "application/json");
var res = await client.GetAsync("http://localhost:00000/home/testGet?username=" + username);
var post = await client.PostAsync("http://localhost:00000/home/testPost?username=" + username, content );

由于HttpContext.Request.ContentLength变化,内容被转移取决于我给它的json ID。

1 个答案:

答案 0 :(得分:1)

您可能需要执行以下操作:

[HttpPost]
public ActionResult testPost([FromUri]string username, TestClass testClass)
{
    var t = new TestClass() { Id = "333", Name = "name" };
    return Json(t, JsonRequestBehavior.AllowGet);
}