用于参数和验证的 C# WCF REST 强类型 WebGet 对象
您可以像在 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)
其中 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 REST 中。我的方法看起来不像:
[WebGet]
public IEnumerable<ObjectResult> SomeAction(string firstname, string lastname, string address, int phone)
我想要以下内容:
[WebGet]
public IEnumerable<ObjectResult> SomeAction(UserObject obj)
这在 WCF REST 4.0 中可能吗?
Can you do the below in WCF 4.0 Rest like you can in ASP.NET MVC?
In ASP.NET MVC I can create a strongly typed object commonly known as a ViewModel to handle error validation.
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:
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.
So in 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认 WCF 无法做到这一点。您必须使用
IDispatchMessageFormatter
的自定义实现来创建自定义行为,以从查询字符串收集参数并构建对象。 这是一个示例如何构建这样的行为和格式化程序。这就像您必须为 ASP.NET MVC 中的每个自定义 ViewModel 编写自定义模型绑定程序一样。顺便提一句。也没有内置逻辑只允许您调用验证(如 MVC 中的 Model.IsValid )。您将需要手动使用与数据注释一起使用的基础结构类 (
System.ComponentModel.DataAnnotations.Validator
)。Default WCF is not able to do that. You must create custom behavior with custom implementation of
IDispatchMessageFormatter
to collect parameters from query string and build the object. 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.Btw. there is also no build in logic which would simply allow you to call validation (like
Model.IsValid
in MVC). You will need to use infrastructure classes used with data annotations manually (System.ComponentModel.DataAnnotations.Validator
).