方法不允许+在WebInvoke

时间:2015-12-01 12:18:47

标签: wcf

我有一个WCF服务,其中我有一个返回任何字符串的方法。

 [ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method="Get", UriTemplate="api/GetData/{name}", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string GetData(string name);

    [OperationContract]
    [WebInvoke(Method = "Post", UriTemplate = "api/PutData", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string PutData(string name);
}

当我点击服务网址(http://localhost:56075/Service.svc/api/GetData/gt)时。它显示错误“方法不允许”你能告诉我这是什么问题..

网页配置如下:

    <?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Hello">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false"/>

        </behavior>

      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp/>
        </behavior>

      </endpointBehaviors>
    </behaviors>

    <services>
      <service name="Ajax.Service" behaviorConfiguration="Hello" >
        <endpoint address="http://localhost:56075/Service.svc" binding="webHttpBinding"  contract="Ajax.IService" behaviorConfiguration="webHttp"></endpoint>

      </service>

    </services>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false"/>
  </system.serviceModel>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
    

1 个答案:

答案 0 :(得分:0)

更改行:

  

[WebInvoke(Method =“Get”,UriTemplate =“api / GetData / {name}”,ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json)]

  

[WebInvoke(Method =“Get”,UriTemplate =“api / GetData?name = {name}”,ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json)]

再试一次。