我有一个asp.net MVC控制器(不是Web api,不是核心),我正在尝试发布一个也具有uri变量的帖子。但我收到了。
找不到FromBody是因为您缺少using指令,还是 组装参考
与FromUri相同
using CFF.CareCenterPortal.BeInCharge.Services;
using CFF.CareCenterPortal.BeInCharge.ViewModels;
using CFF.CareCenterPortal.Web.Controllers.Portal;
using System;
using System.Web.Mvc;
namespace CFF.CareCenterPortal.Web.Controllers.BeInCharge
{
[Authorize]
[RoutePrefix("beincharge/caregiver")]
public class BeInChargeCaregiverController : IdentityController
{
[HttpPost]
[Route("{id}")]
public ActionResult EditCaregiver([FromBody()] CareGiverViewModel data, [FromUri()] int id)
{
var service = new BeInChargeDataService();
var result = service.EditCaregiver(data,id,CurrentUser.NameIdentitifier);
if (result == null)
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.InternalServerError, "An Unhandled Error Occcured");
}
if (!String.IsNullOrEmpty(result.Error) && !String.IsNullOrWhiteSpace(result.Error))
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest, result.Error);
}
return Json("Success");
}
答案 0 :(得分:2)
我可以在.net MVC中使用FromBody和FromUri吗?
不。据我了解,这些属性只是WebAPI约定。您可以选择使用WebAPI Controller(非常简单),也可以编写自己的Custom Model Binder for MVC来检查该参数的属性以模仿您的需求。
我不确定您是否知道,但是MVC ModelBinder已经从Route,QueryString和Body中获取值以具体化参数。
欢迎您查看Source Code to MVC。最重要的是ValueProviderFactories,它具有以下方法:
private static readonly ValueProviderFactoryCollection _factories
= new ValueProviderFactoryCollection()
{
new ChildActionValueProviderFactory(),
new FormValueProviderFactory(),
new JsonValueProviderFactory(),
new RouteDataValueProviderFactory(),
new QueryStringValueProviderFactory(),
new HttpFileCollectionValueProviderFactory(),
new JQueryFormValueProviderFactory()
};
这是MVC用于向模型绑定程序提供值的方法。
示例:
我已经访问了默认MVC网站,并进行了以下更改:
/views/home/Index.cshtml
第8行:
<p><a class="btn btn-primary btn-lg js-test">Learn more »</a></p>
添加到文件底部:
<script src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script>
$(document).ready(function () {
$('.js-test').on('click', function () {
$.ajax({
url: '@Url.RouteUrl(new{ action="Name", controller="Home", id=5})',
data: JSON.stringify({Name: 'My Test Name' }),
type: 'POST',
dataType: 'json',
contentType: "application/json",
});
})
});
</script>
添加了以下文件:
/Models/Home/TestVM.cs
public class TestVM
{
public string Name { get; set; }
}
更新了控制器:
/Controllers/HomeController.cs:
public ActionResult Name(TestVM test, int id)
{
System.Diagnostics.Debug.WriteLine(test.Name);
System.Diagnostics.Debug.WriteLine(id);
return new EmptyResult();
}
现在,单击按钮时,将发出以下请求:
Request URL: http://localhost:53549/Home/Name/5
Request Method: POST
Status Code: 200 OK
Remote Address: [::1]:53549
Referrer Policy: no-referrer-when-downgrade
Cache-Control: private
Content-Length: 0
Date: Tue, 03 Jul 2018 17:21:55 GMT
Server: Microsoft-IIS/10.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 23
Content-Type: application/json
Host: localhost:53549
Origin: http://localhost:53549
Pragma: no-cache
Referer: http://localhost:53549/
{Name: "My Test Name"}
Name
:
"My Test Name"
“我的调试”窗口打印:
我的测试名称
5
My Test Name
是JsonValueProviderFactory
的ModelBound,来自网址http://localhost:53549/Home/Name/5
的id是RouteDataValueProviderFactory
的ModelBound。