WebGet方法在WCF REST服务应用程序中提供错误400“错误请求”

时间:2011-01-19 07:31:50

标签: .net wcf rest

  

可能重复:
  Bad Request 400 while accessing WCF Rest service (WebGet)

大家好,

让我解释一下我在使用WCF休息服务做什么。这是我的代码和配置:

  1. IDNNService界面

    [ServiceContract]
    public interface IDNNService
    {
        [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "hello/{Name}")]
        [OperationContract]
        string SayHello(string Name);
    }
    
  2. 实现界面的
  3. 服务类

    public class DNNService : IDNNService
    {
        public string SayHello(string Name)
        {
            return string.Format("Hello {0}", Name);
        }
    }
    
  4. 配置文件

    <system.serviceModel>
       <services>
          <service behaviorConfiguration="DNNServiceBehavior" name="DNNService">
             <endpoint 
                    address="" 
                    binding="wsHttpBinding" 
                    contract="IDNNService">
                <identity>
                   <dns value="localhost" />
                </identity>
             </endpoint>
             <endpoint 
                    address="mex" 
                    binding="mexHttpBinding"  
                    contract="IMetadataExchange" />
             <endpoint 
                    address="rest" 
                    binding="webHttpBinding" 
                    behaviorConfiguration="httpBehavior" 
                    contract="IDNNService"/>
          </service>
       </services>
       <behaviors>
          <serviceBehaviors>
             <behavior name="DNNServiceBehavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
             </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
              <behavior name="httpBehavior">
                 <webHttp/>
              </behavior>
          </endpointBehaviors>
       </behaviors>
    </system.serviceModel>
    
  5. 我能够使用wsdl

    生成返回正确信息的SVC文件

    但问题是当我尝试访问我的实际方法时

    http://localhost/DC560X_rest/DesktopModules/DNNCentric-RestService/Entities/DNNService.svc/rest/hello/prabhakar
    
    1. IE显示“400 Bad Request”
    2. Firefox显示空白页
    3. 请帮帮我。 提前致谢

1 个答案:

答案 0 :(得分:2)

您的网址看起来像松鼠一样坚固。

设置服务的URL

<system.serviceModel>
   <services>
      <service behaviorConfiguration="DNNServiceBehavior" name="DNNService">
         <endpoint 
                address="http://*:12345/Derp" 
                binding="wsHttpBinding" 
                contract="IDNNService">
            <identity>

(您可能必须确保端口12345未被防火墙阻止)

然后使用网址:http://localhost:12345/Derp(当然,从安装它的同一台计算机上)来测试您的服务。