Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass multiple parameter in wcf restful service?

Tags:

rest

c#

asp.net

wcf

IService.cs

[OperationContract]
[WebGet(UriTemplate = "/IsValidUser?userid={userid}&password={password}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string IsValidUser(string userid, string password);

Service.cs

public string IsValidUser(string userid, string password)
{
    if (userid =="bob" && password =="bob")
    {
        return "True";
    }
    else
    {
        return "false";
    }
}

web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Service" behaviorConfiguration="ServBehave">
        <!--Endpoint for REST-->
        <endpoint address="rest" binding="webHttpBinding" behaviorConfiguration="restPoxBehavior" contract="IService"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServBehave">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <!--Behavior for the REST endpoint for Help enability-->
        <behavior name="restPoxBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

Problem:

Here my problem is that I want to pass multiple parameter while calling a WCF rest service, but I am not able to pass multiple parameters to that WCF rest service. I want to pass userid and password and check for in it. If it is bob then allow to go ahead.

And when I am calling this url:

http://localhost:48401/ARService/Service.svc/rest/IsValidUser?userid=bob&password=bob

then I am getting this error on my web page:

Endpoint not found. Please see the service help page for constructing valid requests to the service.

If somebody have idea how to call IsValidUser in my case with this function parameter. Please help me.

like image 990
Govinda Rajbhar Avatar asked Feb 07 '14 09:02

Govinda Rajbhar


2 Answers

you can write this way:

Iservice.cs

[OperationContract]
[WebGet(UriTemplate = "IsValidUser/{userid}/{password}")]
string IsValidUser(string userid, string password);

service .cs

public string IsValidUser(string userid, string password)
{
   if (userid== "root" && password== "root")
   {
      return "True";
   }
   else
   {
      return "false";
   }    
}

Run this Url in Browser,then you will get output localhost/service.svc/rest/IsValidUser/root/root

like image 66
pankeel Avatar answered Oct 14 '22 06:10

pankeel


try this

[OperationContract]
[WebGet(UriTemplate = "IsValidUser?userid={userid}&password={password}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string IsValidUser(string userid, string password);
like image 29
user6482027 Avatar answered Oct 14 '22 04:10

user6482027