4

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
jray0039
  • 130
  • 1
  • 8

1 Answers1

2

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).

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Bummer. Thanks for the explanation! I was actually going to use enterprise library 5.0 validation block but for simplicity, I left the details out of the post. Thanks again! – jray0039 Aug 31 '11 at 16:06