简体   繁体   English

如何将参数从MVC5项目传递到Rest WCF服务

[英]How pass parameter to Rest WCF Service from MVC5 project

I have a WCF Rest Service(using Json) that gets the username and password and returns Customer information. 我有一个WCF Rest Service(使用Json),该服务获取用户名和密码并返回客户信息。 This is the Method interface. 这是方法界面。

   //Get Customer by Name
    [OperationContract]
    [WebInvoke
       (UriTemplate = "/GetCustomerByName",
        Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json
        )]
    List<Model.Customer> GetCustomerByName(Model.CustomerName CustomerData);

I need to call this method in MVC5 and pass parameter to it. 我需要在MVC5中调用此方法并将参数传递给它。 Not sure how to pass parameter. 不确定如何传递参数。

This is how I call the service: 这就是我所说的服务:

readonly string customerServiceUri = "http://localhost:63674/CSA.svc/";
 public ActionResult SearchByName(InputData model)
    {
        List<CustomerModel> customerModel = new List<CustomerModel>();

        if (ModelState.IsValid)
        {
            if (model != null)
            {
                using (WebClient webclient = new WebClient())
                {
                    string jsonStr = webclient.DownloadString(string.Format("{0}GetCustomerByName?CustomerData={1}", customerServiceUri, model));

                    if (!string.IsNullOrWhiteSpace(jsonStr))
                    {
                        var result = JsonConvert.DeserializeObject<Models.CustomerModel.Result>(jsonStr);

                        if (result != null)
                        {
                            customerModel = result.GetCustomersByNameResult;
                        }
                    }
                }
            }
        }
        return View(customerModel);
    }

I am getting error right on this line: 我在这条线上出现错误:

 string jsonStr = webclient.DownloadString(string.Format("{0}GetCustomerByName?CustomerData={1}", customerServiceUri, model));

and this is the error: 这是错误:

The remote server returned an error: (405) Method Not Allowed. 远程服务器返回错误:(405)不允许使用方法。

and this is InputData class: 这是InputData类:

 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 the service, wrong. 问题在于代码行正在调用服务,这是错误的。 Because you pass the values in url, the line of code is making an GET request, not a POST . 因为您在url中传递值,所以代码行正在发出GET请求, 而不是 POST If you are willing to make a POST request please follow this answer . 如果您愿意提出POST请求,请遵循此答案

What is wrong with the code? 代码有什么问题?

string jsonStr = webclient.DownloadString(string.Format("{0}GetCustomerByName?CustomerData={1}", customerServiceUri, model));

1) This error is thrown (405) Method Not Allowed because you expecting because a POST request is expected and is made a GET request. 1)引发此错误(405) Method Not Allowed because you expecting因为(405) Method Not Allowed because you expecting POST请求并将其作为GET请求。

2) This will output something like this: http://localhost:63674/CSA.svc/GetCustomerByName?CustomerData=[SolutionName].[ProjectName].InputData 2)这将输出如下内容: http://localhost:63674/CSA.svc/GetCustomerByName?CustomerData=[SolutionName].[ProjectName].InputData

This is happening because C# don't know how to convert InputData to string in the way you want, for this you have to override the method ToString() . 发生这种情况是因为C#不知道如何以所需的方式将InputData转换为字符串,为此,您必须重写方法ToString()

Possible solution 可能的解决方案

Try to make a GET request, you have to call the service in this way (with few modifications) 尝试发出GET请求,您必须以这种方式(很少修改)调用服务

string jsonStr = webclient.DownloadString(string.Format("{0}GetCustomerByName?firstName={1}&lastName={2}", customerServiceUri, model.First_Name, model.Last_Name));

You have to modify the service to match the example i made for the GET request. 您必须修改服务以匹配我为GET请求所做的示例。

//Get Customer by Name
[OperationContract]
[WebGet(UriTemplate = "GetCustomerByName?firstName={firstName}&lastName={lastName}")]
List<Model.Customer> GetCustomerByName(string firstName, string lastName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM