【问题标题】:How pass parameter to Rest WCF Service from MVC5 project如何将参数从 MVC5 项目传递给 Rest WCF 服务
【发布时间】:2015-03-17 18:22:37
【问题描述】:

我有一个 WCF 休息服务(使用 Json),它获取用户名和密码并返回客户信息。这是方法接口。

   //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);

我需要在 MVC5 中调用这个方法并将参数传递给它。不知道如何传递参数。

这就是我调用服务的方式:

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);
    }

我在这一行遇到错误:

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

这是错误:

远程服务器返回错误:(405) Method Not Allowed。

这是 InputData 类:

 public class InputData
  {

      public string First_Name { get; set; }
      public string Last_Name { get; set; }
  }

【问题讨论】:

  • 为什么你使用Method = "POST"而不是Method = "GET"
  • @adricadar 不确定我是否认为我正在向其发布用户名和密码。我将其更改为 Get 并收到此错误:“远程服务器返回错误:(500) 内部服务器错误。”
  • 我问为什么不GET,因为从你的方法名GetCustomerByName 我继承了你想发出GET 请求。
  • 你也得到了(405) Method Not Allowed,因为你期待一个POST并创建一个GET

标签: c# asp.net-mvc wcf rest asp.net-mvc-5


【解决方案1】:

问题是这行代码调用了服务,错了。因为您在 url 中传递值,所以这行代码发出一个 GET 请求,不是一个 POST。如果您愿意提出POST 请求,请follow this answer

代码有什么问题?

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

1) 抛出此错误(405) Method Not Allowed because you expecting,因为需要POST 请求并发出GET 请求。

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

发生这种情况是因为 C# 不知道如何以您想要的方式将 InputData 转换为字符串,为此您必须覆盖方法 ToString()

可能的解决方案

尝试发出GET请求,你必须以这种方式调用服务(稍作修改)

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

您必须修改服务以匹配我为GET 请求制作的示例。

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

【讨论】:

  • 再次出现同样的错误,当我使用 Post 时出现此错误。远程服务器返回错误:(405) Method Not Allowed。当我使用 Get 时,我收到此错误:“远程服务器返回错误:(500)内部服务器错误。”
  • @nikoom 如果你想创建一个GET,你必须修改服务方法,我将编辑我的答案。如果您愿意提出POST 请求,请遵循以下答案:stackoverflow.com/questions/20651645/…
  • 无法使用此功能。然后服务给了我这个错误。合同“ICSA”中的“GetCustomerByName”操作使用 GET,但也有主体参数“firstName”。 GET 操作不能有主体。将参数“firstName”设为 UriTemplate 参数,或从 WebGetAttribute 切换到 WebInvokeAttribute。
猜你喜欢
  • 1970-01-01
  • 2012-01-07
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多