如何在WCF RESTful服务中传递多个参数?

17

IService.cs

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

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

问题:

我的问题是在调用WCF REST服务时,我想传递多个参数,但我无法将多个参数传递给该WCF REST服务。我想传递userid和password并进行检查。如果它是bob,则允许继续执行。

当我调用此URL时:

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

我的网页显示以下错误:

找不到端点。请参阅服务帮助页面以构建对服务的有效请求。

如果有人知道如何在我的情况下调用具有此函数参数的IsValidUser,请帮帮我。


除了明文传输的密码外,您没有提到实际问题是什么。您无法传递参数吗?为什么?您遇到了什么问题,收到了什么错误消息? - nvoigt
你的URL和代码完全不符。也许在考虑参数和函数之前,你应该先确保你的服务是可调用的。 - nvoigt
@silvermind 抱歉,我编辑了我的 URL。 - Govinda Rajbhar
@GovindaRajbhar 我怀疑这与您的函数或参数无关。尝试插入一个没有任何参数的函数,这也会失败。确保URL正确,并正确配置了端点。您能从svc请求WSDL吗? - nvoigt
显示剩余3条评论
3个回答

32

你可以这样写:

Iservice.cs

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

服务.cs

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

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


如何在域名后传递用户ID?例如“domain\userid”。由于参数中包含“\”字符,因此会出现错误请求错误。 - Ali
谢谢 @pankee - Ishwor Khanal

5

尝试这个。

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

2

Add BodyStyle on OperationContract

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接