【发布时间】: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