【问题标题】:How to pass multiple parameter in wcf restful service?如何在 wcf RESTful 服务中传递多个参数?
【发布时间】:2014-03-04 14:38:31
【问题描述】:

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>

问题:

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

当我调用这个网址时:

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

然后我在我的网页上收到此错误:

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

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

【问题讨论】:

  • 除了明文传输的密码外,您没有提及您的实际问题是什么。你是不是不能传参数?为什么不?您遇到了哪些问题,收到了哪些错误消息?
  • 当我调用这个 url 时:-localhost:1967/RestService/callme?id=bob&value=bob 然后我得到这个错误:找不到端点。请参阅服务帮助页面以构建对服务的有效请求。在我的网页上。
  • 您的网址与您的代码完全不匹配。也许你应该先确保你的服务是可调用的,然后再考虑参数和函数。
  • 您的参数名称是useridpassword,但您发送的是idvalue
  • @silvermind 抱歉我编辑了我的网址

标签: c# asp.net wcf rest


【解决方案1】:

你可以这样写:

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" 由于参数中的 "\" 字符,我收到错误的请求错误。
  • 谢谢@pankee
【解决方案2】:

试试这个

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

【讨论】:

    【解决方案3】:

    在 OperationContract 上添加 BodyStyle

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

    【讨论】:

      猜你喜欢
      • 2015-06-03
      • 2016-05-18
      • 2011-10-10
      • 2012-12-04
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      相关资源
      最近更新 更多