gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:13:45 27 4
gpt4 key购买 nike

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,则允许继续。

当我调用这个 url 时:

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

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

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

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

最佳答案

可以这样写:

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

关于c# - 如何在 wcf restful 服务中传递多个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21623432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com