避免空模型时,没有数据被张贴在网页API [英] Avoid null model when no data is posted in Web API
问题描述
这个问题是相似,我想实现:
This question is similar to what I want to achieve:
<一个href=\"http://stackoverflow.com/questions/25160345/avoiding-null-model-in-asp-net-web-api-when-no-posted-properties-match-the-model\">Avoiding在ASP.Net的Web API空模型时没有发表任何属性的模型匹配
但它的消失未回答。
我有这需要一个模型,是一个GET路线:
I have a route which takes a model that is a GET:
[HttpGet, Route("accounts")]
public AccountListResult Post(AccountListRequest loginRequest)
{
return accountService.GetAccounts(loginRequest);
}
该模型中填充额外的数据,从一个动作过滤器。
The model is populated with additional data from an action filter.
在需要知道这种情况下,所有的用户ID,该行动过滤器添加的基于cookie的/头模型与请求传递。
In this case all that needs to be known is the UserId, which the action filter add's to the model based on cookie/header into passed in with the request.
我想用所有的默认模型中的WebAPI约束力,但我想避免空的对象。
I want to use all the default model binding in WebAPI but I want to avoid null objects.
我不相信模型的结合解决了我的问题。
I don't believe model binding solves my problem.
<一个href=\"http://stackoverflow.com/questions/26527492/how-do-i-replace-the-behaviour-of-web-api-model-binding-so-that-instead-of-null\">How我取代的Web API模型结合,使而不是空的,我收到一个新的实例时没有参数传递的行为
这是接近我想,除了这是乏味的每种类型的事情。
This is closer to what I want to do except its per type which is tedious.
推荐答案
编辑:由于问题是Web API,我张贴也低于网页API的解决方案。
Since the question is for Web API, I am posting the Web API solution also below.
您可以在操作过滤器如下做到这一点。下面code仅如果模型包含默认构造函数。
You can do this as below in an Action Filter. The below code works only if your model contains default constructor.
网络API实现:
public override void OnActionExecuting(HttpActionContext actionContext)
{
var parameters = actionContext.ActionDescriptor.GetParameters();
foreach (var parameter in parameters)
{
object value = null;
if (actionContext.ActionArguments.ContainsKey(parameter.ParameterName))
value = actionContext.ActionArguments[parameter.ParameterName];
if (value != null)
continue;
value = CreateInstance(parameter.ParameterType);
actionContext.ActionArguments[parameter.ParameterName] = value;
}
base.OnActionExecuting(actionContext);
}
protected virtual object CreateInstance(Type type)
{
// Check for existence of default constructor using reflection if needed
// and if performance is not a constraint.
// The below line will fail if the model does not contain a default constructor.
return Activator.CreateInstance(type);
}
MVC实现:
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var parameters = filterContext.ActionDescriptor.GetParameters();
foreach (var parameter in parameters)
{
if (filterContext.ActionParameters.ContainsKey(parameter.ParameterName))
{
object value = filterContext.ActionParameters[parameter.ParameterName];
if (value == null)
{
// The below line will fail if the model does not contain a default constructor.
value = Activator.CreateInstance(parameter.ParameterType);
filterContext.ActionParameters[parameter.ParameterName] = value;
}
}
}
base.OnActionExecuting(filterContext);
}
这篇关于避免空模型时,没有数据被张贴在网页API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!