Sunday, 15 July 2012

c# - How pass parameter to Rest WCF Sevice from MVC5 project -


I have the WCF Race Service (using JSN) that receives the username and password and gives customer information. This method is the interface.

  // name [OperationContract] [WebInvoke Clients (UriTemplate = "/ GetCustomerByName", method = "post", Bodystyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat. Jesn, Anurodformet = Vebmelformat .jason]] List & Model; Model Customers & gt; GetCustomerByName (Model.CustomerNameCustomerData);  

I have to call this method in MVC5 and give it a parameter. Not sure how to pass the parameters.

In this way, I call this service:

  readonly string customerServiceUri = "http: // localhost: 63674 / CSA.svc /"; Public Action Search SearchByName (InputData model) {list & lt; CustomerModel & gt; Customer Model = New List & lt; CustomerModel & gt; (); If (ModelState.IsValid) {if (model! = Null) using {(WebClient webclient = new WebClient ()) {String jsonStr = webclient.DownloadString (String.Format ( "{0} GetCustomerByName? CustomerData = {1} ", Customer service service, model)); If {var results = JsonConvert.DeserializeObject & lt (string.IsNullOhWhiteSpace (jsonStr)!); Models.CustomerModel.Result & gt; (JsonStr); If (result! = Zero) {customerModel = result.GetCustomersByNameResult; }}}}} See Return (Customer Model); }  

I'm getting an error on this line right:

  string jsonStr = webclient.DownloadString (String.Format ("{0} GetCustomerByName CustomerData = {1} ", customer service service, model));  

And this is the error:

Remote server returned an error: (405) method not allowed.

and this is the InputData class:

  public class InputData {public string First_Name {get; Set; } Public string Last_Name {get; Set; The problem is that the line of code is calling service, it is wrong. Since you pass the value in the url, the code line  GET  is making a request,  no  a  POST . If you are ready to request a  post , please 

What's wrong with the code?

  String JsonStr = webclient.DownloadString (string.Format ("{0} GetCustomerByName? CustomerData = {1}", customer service service, model));  

1) This error is not allowed in the (405) method because you expect because a POST request is required And get request

2) so that the output will be something like: http: // localhost:. 63.674 / CSA.svc / GetCustomerByName CustomerData = [SolutionName] [ProjectName] .InputData

This is happening because C # does not know how InputData the way you want, you must override method toString () .

Possible Solutions

Try to request a , you will need to call this service like this ( With some modifications))

  string jsonStr = webclient.DownloadString (string.Format ("{0} GetCustomerByName? First name = {1} and last name = {2}", customer service service Model, first_name, model last name));  

You must modify the service to match the example created for the GET request.

  // Get the customer name [OperationContract] [WebGet (UriTemplate = "GetCustomerByName firstName = {firstName} & lastName = {lastName}")] & lt; Model.Customer & gt; GetCustomerByName (string first name, string last name);  

No comments:

Post a Comment