Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wcf get method - how to bind to view model?

Tags:

c#

wcf

This is what I have in the service contract:

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, 
           BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
string HelloWorld(HelloWorldViewModel vm);

where HelloWorldViewModel has properties X and Y.

If I do localhost/webservices/HelloWorld?X=1&Y=2, and set a breakpoint in the HelloWorld method, vm will be null. It does not automatically bind the passed-in query string params into a view model object.

Am I missing something? Thanks!

like image 661
Ian Davis Avatar asked Dec 13 '25 01:12

Ian Davis


1 Answers

The behavior you described is implemented specifically in ASP.NET MVC model binding.

If you want to pass a complex object to a REST service operation using the WCF Web Programming Model, you'll have to include it in its serialized form in the body of an HTTP POST request.

In your case, based on the attributes placed on the HelloWorld service operation, the request's payload should look something like this (note that XML namespace declarations are omitted):

<HelloWorld>
    <vm>
        <X>1</X>
        <Y>2</Y>
    </vm>
</HelloWorld>

Related resources:

  • WebMessageBodyStyle Enumeration
like image 110
Enrico Campidoglio Avatar answered Dec 15 '25 15:12

Enrico Campidoglio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!