用于参数和验证的c#WCF REST强类型WebGet对象
您能像在ASP.NET MVC中一样在WCF 4.0 Rest中执行以下操作吗 在ASP.NET MVC中,我可以创建一个强类型对象(通常称为ViewModel)来处理错误验证 而不是以下内容:用于参数和验证的c#WCF REST强类型WebGet对象,c#,asp.net-mvc,wcf,rest,C#,Asp.net Mvc,Wcf,Rest,您能像在ASP.NET MVC中一样在WCF 4.0 Rest中执行以下操作吗 在ASP.NET MVC中,我可以创建一个强类型对象(通常称为ViewModel)来处理错误验证 而不是以下内容: public ActionResult SomeAction(string firstname, string lastname, string address, int phone) public ActionResult SomeAction(UserObject obj) [WebGet] p
public ActionResult SomeAction(string firstname, string lastname, string address, int phone)
public ActionResult SomeAction(UserObject obj)
[WebGet]
public IEnumerable<ObjectResult> SomeAction(UserObject obj)
我可以有以下几点:
public ActionResult SomeAction(string firstname, string lastname, string address, int phone)
public ActionResult SomeAction(UserObject obj)
[WebGet]
public IEnumerable<ObjectResult> SomeAction(UserObject obj)
其中,UserObject定义为:
public class UserObject
{
[Required(ErrorMessage = "firstname is a required paramater")]
public string firstname { get; set; }
[StringLength(50, ErrorMessage = "lastname is too long")]
public string lastname { get; set; }
[StringLength(160)]
public string address { get; set; }
public int phone { get; set; }
}
我基本上想做的是在强类型对象中创建参数,并在其中显示错误消息。然后,我可以将错误消息格式化为xml并将其返回给用户
所以在WCF休息。而不是我的方法看起来像:
[WebGet]
public IEnumerable<ObjectResult> SomeAction(string firstname, string lastname, string address, int phone)
[WebGet]
public IEnumerable SomeAction(string firstname、string lastname、string address、int phone)
我希望:
public ActionResult SomeAction(string firstname, string lastname, string address, int phone)
public ActionResult SomeAction(UserObject obj)
[WebGet]
public IEnumerable<ObjectResult> SomeAction(UserObject obj)
[WebGet]
公共IEnumerable SomeAction(UserObject obj)
这在WCF REST 4.0中可能吗?默认WCF无法做到这一点。您必须使用
IDispatchMessageFormatter
的自定义实现创建自定义行为,以从查询字符串收集参数并生成对象。如何构建这样的行为和格式化程序。这就像您必须为ASP.NET MVC中的每个自定义ViewModel编写自定义模型绑定器一样
顺便说一句,也没有内置逻辑,只允许您调用验证(如MVC中的
Model.IsValid
)。您需要手动使用与数据批注一起使用的基础结构类(System.ComponentModel.DataAnnotations.Validator
)。Bummer。谢谢你的解释!我实际上打算使用EnterpriseLibrary5.0验证块,但为了简单起见,我在文章中没有提到细节。再次感谢!