我正在创建一个AngularJS Web表单,使用WebAPI设置作为OData对表执行POST(插入)。我试图返回一个失败的验证ModelState对象(以JSON格式)以验证表单上的相应字段。
我得到的所有内容都是一个字符串,所有细节都以字符串形式显示(不是JSON可解析格式)
{
"odata.error":{
"code":"","message":{
"lang":"en-US","value":"The request is invalid."
},"innererror":{
"message":"application.ApplicationName : The ApplicationName field is required.\r\n","type":"","stacktrace":""
}
}
}
我的帖子方法如下:
public async Task<IHttpActionResult> Post(Application application)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Applications.Add(application);
await db.SaveChangesAsync();
return Created(application);
}
我甚至尝试将其抽象为ActionFilterAttribute,但仍然是相同的结果
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
var modelState = actionContext.ModelState;
if (!modelState.IsValid)
actionContext.Response = actionContext.Request
.CreateErrorResponse(HttpStatusCode.BadRequest, modelState);
}
}
}
我的WebApi启动方法具有以下配置:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Application>("DataApplications");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Include };
Configure(config);
config.EnableQuerySupport();
// Use camel case for JSON data.
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
这就是我想要实现的目标(与上面的例子不符):
{
"Message": "The request is invalid.",
"ModelState": {
"car": [
"Required property 'Make' not found in JSON. Path '', line 1, position 57."
],
"car.Make" : [
"The Make field is required."
],
"car.Price": [
"The field Price must be between 0 and 200000."
]
}
}
我需要返回ModelState,以便在相应的字段中定位验证反馈。
我可以检查/更改任何想法以使其按预期工作吗?
感谢。
更新1 - 在asp.net上找到的参考
OData错误响应不包含模型状态错误
使用CreateErrorResponse扩展方法或HttpErrors直接创建错误响应时,错误将映射到OData错误响应。错误响应中的任何模型状态错误都不会传播到OData错误响应。要保留OData错误响应中的模型状态错误,请直接使用CreateODataErrorResponse扩展方法或ODataError,并将模型状态错误的描述添加到OData错误消息中。
答案 0 :(得分:0)
OData使用<div class="container-fluid block-events" style="width: 1230px!important;">
<div class="content">
<div class="wrap">
<div id="main" role="main" style="min-height:1200px!important;">
<h1 class="clearfix"><?php echo $lang['koikuudised']; ?></h1>
<ul id="tiles">
<?php while($gallery = mysqli_fetch_array($db)) {
?>
<li>
<figure class="effect-marley">
<a title="<?php echo $gallery['gallery_thumbnail']; ?>" data-lightbox="gallery-popup" href="<?php echo $CONF['installation_path'] . $gallery['gallery_thumbnail']; ?>">
<img src="<?php echo $CONF['installation_path'] . 'system/timthumb.php?src=' . $CONF['installation_path'] . $gallery['gallery_thumbnail'] . '&h=350&zc=1'; ?>" alt="<?php echo $gallery['gallery_name']; ?>" />
</a>
<figcaption>
<div class="brown">
<p><?php echo $gallery['gallery_name']; ?></p>
</div>
</figcaption>
</figure>
</li>
<?php } ?>
</ul>
</div>
</div>
</div>
<div style="clear:both;"></div>
</div>
作为错误响应。 ODataError
和ODataError
之间的区别在于HttpError派生自HttpEror
所以当它与Dictionary<string, object>
实例化时,所有模型错误都设置为ModelStateDictionary
属性。但是,当您请求OData时,ModelState
对象将映射到HttpError
,并且所有验证错误都会连接到ODataError
属性。