我点击这个链接Supporting OData Actions in ASP.NET Web API
我想将我的对象/实体作为这样的参数传递:
ActionConfiguration addNewPatient = builder.Entity<Patient>().Collection.Action("AddNewPatient");
addNewPatient.Parameter<int>("hospId");
addNewPatient.Parameter<int>("docId");
addNewPatient.Parameter<Patient>("patient");
addNewPatient.Returns<bool>();
但我有这个问题:
System.ArgumentException: Invalid parameter type 'Patient'.
A non-binding parameter type must be either Primitive, Complex, Collection of Primitive or a Collection of Complex.
Parameter name: parameterType
我试图实现这个
ActionConfiguration addNewPatient = builder.Entity<Patient>().Collection.Action("AddNewPatient");
addNewPatient.Parameter<int>("hospId");
addNewPatient.Parameter<int>("docId");
var patientConfig = builder.StructuralTypes.OfType<EntityTypeConfiguration>().Single(x => x.Name == "Patient");
addNewPatient.SetBindingParameter("patient", patientConfig, false);
addNewPatient.Returns<bool>();
但我不能再调用方法POST ../odata/Patient/AddNewPatient
<FunctionImport Name="AddNewPatient" ReturnType="Edm.Boolean" IsBindable="true">
<Parameter Name="patient" Type="Patient"/>
<Parameter Name="hospId" Type="Edm.Int32" Nullable="false"/>
<Parameter Name="docId" Type="Edm.Int32" Nullable="false"/>
</FunctionImport>
请帮助我,我尝试了各种方法,但还是没有运气.
谢谢.
解决方法:
您可以使用ActionConfiguration.EntityParameter()方法将实体作为参数绑定到OData操作方法.
这是一个例子:
ActionConfiguration validate = ModelBuilder.EntityType<TEntity>()
.Collection.Action("Validate");
validate.Namespace = "Importation";
validate.EntityParameter<TEntity>(typeof(TEntity).Name);
validate.CollectionParameter<string>("UniqueFields");
validate.Returns<ValidationResult>();
但是,请注意,ModelState不会检查提供的Entity的内容,而是将所有缺少的属性设置为null,并且模型中超过StringLength(x)注释的属性仍将通过.如果您想在之后验证实体本身,请在您的操作方法中使用以下代码:
[HttpPost]
public virtual IHttpActionResult Validate(ODataActionParameters parameters)
{
//First we check if the parameters are correct for the entire action method
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
else
{
//Then we cast our entity parameter in our entity object and validate
//it through the controller's Validate<TEntity> method
TEntity Entity = (TEntity)parameters[typeof(TEntity).Name];
Validate(Entity, typeof(TEntity).Name);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
IEnumerable<string> uniqueFields = parameters["UniqueFields"] as IEnumerable<string>;
bool result = Importer.Validate(Entity, uniqueFields);
return Ok(result);
}
}