[英]c# WCF REST strongly typed WebGet object for parameters and validation
Can you do the below in WCF 4.0 Rest like you can in ASP.NET MVC? 您可以像在ASP.NET MVC中一样在WCF 4.0 Rest中执行以下操作吗?
In ASP.NET MVC I can create a strongly typed object commonly known as a ViewModel to handle error validation. 在ASP.NET MVC中,我可以创建一个强类型对象(通常称为ViewModel)来处理错误验证。
Instead of the following: 代替以下内容:
public ActionResult SomeAction(string firstname, string lastname, string address, int phone)
I could have the following: 我可以有以下内容:
public ActionResult SomeAction(UserObject obj)
Where UserObject is defined as: 其中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; }
}
What I am basically wanting to do is create the parameters in a strongly typed object and have my error messages there. 我基本上想要做的是在一个强类型的对象中创建参数,并在其中存储我的错误消息。 I could then format the error message as xml and return it to the user.
然后,我可以将错误消息格式化为xml并将其返回给用户。
So in WCF REST. 因此在WCF REST中。 Instead of My method looking like:
而不是我的方法看起来像:
[WebGet]
public IEnumerable<ObjectResult> SomeAction(string firstname, string lastname, string address, int phone)
I want the following: 我想要以下内容:
[WebGet]
public IEnumerable<ObjectResult> SomeAction(UserObject obj)
Is this possible in WCF REST 4.0? 在WCF REST 4.0中可能吗?
Default WCF is not able to do that. 默认WCF无法执行此操作。 You must create custom behavior with custom implementation of
IDispatchMessageFormatter
to collect parameters from query string and build the object. 您必须使用
IDispatchMessageFormatter
自定义实现来创建自定义行为,以从查询字符串中收集参数并构建对象。 Here is an example how to build such behavior and formatter. 这是一个如何构建这种行为和格式化程序的示例 。 It would be like if you have to write custom model binder for each custom ViewModel in ASP.NET MVC.
就像您必须为ASP.NET MVC中的每个自定义ViewModel编写自定义模型绑定程序一样。
Btw. 顺便说一句。 there is also no build in logic which would simply allow you to call validation (like
Model.IsValid
in MVC). 也没有内置的逻辑可以简单地调用验证(例如MVC中的
Model.IsValid
)。 You will need to use infrastructure classes used with data annotations manually ( System.ComponentModel.DataAnnotations.Validator
). 您将需要手动使用与数据注释一起使用的基础结构类(
System.ComponentModel.DataAnnotations.Validator
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.