REST 服务无法正常工作 400 错误请求

发布于 2024-11-26 11:41:38 字数 5299 浏览 0 评论 0原文

这是我的 web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="WCFRestExample.HelloWorld">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:60503"/>
          </baseAddresses>

        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WCFRestExample.IHelloWorld"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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="true" />

        </behavior>

      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <!--<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="RequestBlockingRule1" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" />
                    <conditions>
                        <add input="{URL}" pattern="*.aspx" />
                    </conditions>
                    <action type="Redirect" url="http://localhost/WCFRestExample/CustomError" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>-->
  <connectionStrings>
    <add name="PubsEntities" connectionString="metadata=res://*/PubsModel.csdl|res://*/PubsModel.ssdl|res://*/PubsModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=.\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

这是我的实际服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;

namespace WCFRestExample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class HelloWorld : IHelloWorld
    {
        [WebGet(UriTemplate="/GetData", ResponseFormat=WebMessageFormat.Xml)]
        public string GetData()
        {
            return "HIIII";
        }

        [WebGet(UriTemplate = "/GetPublisherList", ResponseFormat = WebMessageFormat.Xml)]
        public List<publisher> GetPublisherList()
        {
            using (PubsEntities entities = new PubsEntities())
            {
                return entities.publishers.ToList();
            }
        }
    }
}

这是我的界面:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;

namespace WCFRestExample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IHelloWorld
    {

        [OperationContract]
        string GetData();

        [OperationContract]
        List<publisher> GetPublisherList();

    }

}

这是我的 svc 标记:

<%@ ServiceHost Language="C#" Debug="true" Service="WCFRestExample.HelloWorld" 
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
CodeBehind="HelloWorld.svc.cs" %>

如果我从 web.config 中删除这一部分,它可以正常工作:

 <service name="WCFRestExample.HelloWorld">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:60503"/>
          </baseAddresses>

        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WCFRestExample.IHelloWorld"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>

但是该方法返回 List 仍然不起作用。我正在使用 EF 4.1。谁能告诉我发生了什么事吗?

提前致谢 :)

This is my web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="WCFRestExample.HelloWorld">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:60503"/>
          </baseAddresses>

        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WCFRestExample.IHelloWorld"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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="true" />

        </behavior>

      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <!--<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="RequestBlockingRule1" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" />
                    <conditions>
                        <add input="{URL}" pattern="*.aspx" />
                    </conditions>
                    <action type="Redirect" url="http://localhost/WCFRestExample/CustomError" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>-->
  <connectionStrings>
    <add name="PubsEntities" connectionString="metadata=res://*/PubsModel.csdl|res://*/PubsModel.ssdl|res://*/PubsModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;Initial Catalog=pubs;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

This is my actual service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;

namespace WCFRestExample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class HelloWorld : IHelloWorld
    {
        [WebGet(UriTemplate="/GetData", ResponseFormat=WebMessageFormat.Xml)]
        public string GetData()
        {
            return "HIIII";
        }

        [WebGet(UriTemplate = "/GetPublisherList", ResponseFormat = WebMessageFormat.Xml)]
        public List<publisher> GetPublisherList()
        {
            using (PubsEntities entities = new PubsEntities())
            {
                return entities.publishers.ToList();
            }
        }
    }
}

This is my interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.ServiceModel.Activation;

namespace WCFRestExample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IHelloWorld
    {

        [OperationContract]
        string GetData();

        [OperationContract]
        List<publisher> GetPublisherList();

    }

}

This is my markup of svc:

<%@ ServiceHost Language="C#" Debug="true" Service="WCFRestExample.HelloWorld" 
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
CodeBehind="HelloWorld.svc.cs" %>

If I remove this section from web.config, it works fine:

 <service name="WCFRestExample.HelloWorld">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:60503"/>
          </baseAddresses>

        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WCFRestExample.IHelloWorld"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>

But the method that returns List still doesn't work. I am using EF 4.1. Can anybody tell me what is happening?

Thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2024-12-03 11:41:38

将绑定从 basicHttpBinding 更改为 webHttpBinding。如果你想做REST,你需要使用webHttpBinding。

Change the binding from basicHttpBinding to webHttpBinding. If you want to do REST, you need to use the webHttpBinding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文