Reputation: 4420
I have a WCF Rest Service(using Json) that gets the username and password and returns Customer information. 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. 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.
and this is InputData class:
public class InputData
{
public string First_Name { get; set; }
public string Last_Name { get; set; }
}
Upvotes: 2
Views: 1073
Reputation: 10219
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
. If you are willing to make a POST
request please follow this answer.
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.
2) This will output something like this: 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()
.
Possible solution
Try to make a GET
request, you have to call the service in this way (with few modifications)
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 Customer by Name
[OperationContract]
[WebGet(UriTemplate = "GetCustomerByName?firstName={firstName}&lastName={lastName}")]
List<Model.Customer> GetCustomerByName(string firstName, string lastName);
Upvotes: 1