C# 使用WCF Rest从URL获得Json格式响应

C# 使用WCF Rest从URL获得Json格式响应,c#,json,wcf,C#,Json,Wcf,我想用POST方法从URL获取WCF Json请求。当我在C#中调用函数时,我接受了正确的请求,但当我将函数写入URL时,ı无法获得正确的结果。有人能帮我吗 这是我的Service1.svc文件 这是我的web.config文件 <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!-- For more information on Entity

我想用POST方法从URL获取WCF Json请求。当我在C#中调用函数时,我接受了正确的请求,但当我将函数写入URL时,ı无法获得正确的结果。有人能帮我吗

这是我的Service1.svc文件 这是我的web.config文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>



  <system.serviceModel>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web" >
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <services>
      <service behaviorConfiguration="ServiceBehaviour" name="MyServiceApplication.Service1">
        <endpoint address="Service1" binding="basicHttpBinding" contract="MyServiceApplication.IService1" />
      </service>
    </services>

    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <connectionStrings>
    <add name="FoodRecommendationDatabaseEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=ANIL-BILGISAYAR\SQLEXPRESS;initial catalog=FoodRecommendationDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>

</configuration>

您应该将接口更改为

[OperationContract]
[WebInvoke(Method = "POST",
           RequestFormat = WebMessageFormat.Json,
           ResponseFormat = WebMessageFormat.Json,
           UriTemplate = "ChangePassword")]
LoginInformations ChangePassword(LoginInformations loginInformations);
并将json作为主体发送,但不作为查询参数发送


否则,您可以拆分登录名和密码并将其作为查询参数单独发送,但不建议这样做(很容易窃取您的凭据)

请在您的
IService1.svc
文件中尝试此操作

[OperationContract]
[WebInvoke(Method = "POST",
   RequestFormat = WebMessageFormat.Json,
   ResponseFormat = WebMessageFormat.Json,
   UriTemplate = "ChangePassword")]
LoginInformations ChangePassword(LoginInformations loginInformations);

[DataContract]
public class LoginInformations
{
    [DataMember]
    public string UserName { set; get; }
    [DataMember]
    public string Password { set; get; }
}
在您的
Service1.svc
文件中

    public LoginInformations ChangePassword(LoginInformations loginInformations)
    {
        try
        {
            loginInformations.Password = "654321";

            return loginInformations;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

你能显示你不正确的结果吗?我编辑并添加了图片,你能显示不正确的结果吗?你能举一个例子说明ı如何调用Url吗?(比如像这样?@AnilKilic,是的,你也应该发送一个body,所以你需要使用一些应用程序,比如fiddler或plugin for browser,等等,我如何使用这个功能?我应该从url这样称呼它吗?如何从url发送此函数的参数?有人说我应该加小提琴手,但我不知道怎么做
[OperationContract]
[WebInvoke(Method = "POST",
   RequestFormat = WebMessageFormat.Json,
   ResponseFormat = WebMessageFormat.Json,
   UriTemplate = "ChangePassword")]
LoginInformations ChangePassword(LoginInformations loginInformations);

[DataContract]
public class LoginInformations
{
    [DataMember]
    public string UserName { set; get; }
    [DataMember]
    public string Password { set; get; }
}
    public LoginInformations ChangePassword(LoginInformations loginInformations)
    {
        try
        {
            loginInformations.Password = "654321";

            return loginInformations;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }