如何在wcf restful服务中传递多个参数?

时间:2022-12-29 19:35:56

IService.cs

IService.cs

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

Service.cs

Service.cs

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

web.config

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.

这里我的问题是我想在调用WCF休息服务时传递多个参数,但我无法将多个参数传递给该WCF休息服务。我想传递用户ID和密码并检查它。如果它是bob然后允许继续。

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.

如果有人知道如何使用此函数参数调用IsValidUser。请帮帮我。

3 个解决方案

#1


28  

you can write this way:

你可以这样写:

Iservice.cs

Iservice.cs

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

service .cs

服务.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

在浏览器中运行此Url,然后您将获得输出localhost / service.svc / rest / IsValidUser / root / root

#2


2  

try this

尝试这个

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

#3


1  

Add BodyStyle on OperationContract

在OperationContract上添加BodyStyle

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]

#1


28  

you can write this way:

你可以这样写:

Iservice.cs

Iservice.cs

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

service .cs

服务.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

在浏览器中运行此Url,然后您将获得输出localhost / service.svc / rest / IsValidUser / root / root

#2


2  

try this

尝试这个

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

#3


1  

Add BodyStyle on OperationContract

在OperationContract上添加BodyStyle

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]