【发布时间】:2018-07-04 01:25:12
【问题描述】:
我有一个 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");
}
【问题讨论】:
-
你为什么要这样做?
-
好吧,因为我正在做一个有我的视图模型的帖子。但我们只是要求我们所有传出的 web api 调用也需要另一个变量。 (所以我在帖子的视图模型中有数据,并且我将 id 作为 uri 变量)所以它就像一个带有 uir blah/blah/15 的帖子,我需要 15 以及来自与视图模型相关的帖子
-
MVC ModelBinder 已经做到了。它结合了所有传入的值(Route、QueryString 和 Body)并实现了所有适用的参数。
标签: c# .net asp.net-mvc