C# 最简单的REST服务不工作

C# 最简单的REST服务不工作,c#,wcf,rest,.net-4.0,C#,Wcf,Rest,.net 4.0,有人能告诉我为什么这个休息服务不起作用吗 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. public class HelloWorld : IHelloWorld { [W

有人能告诉我为什么这个休息服务不起作用吗

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.
    public class HelloWorld : IHelloWorld
    {
        [WebGet(UriTemplate="/", ResponseFormat=WebMessageFormat.Xml)]
        public string GetData()
        {
            return "HIIII";
        }
    }
}

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();
    }
}
这是我的web.config:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>
我有404页。有谁能告诉我发生了什么,我在哪里可以找到WCF REST的教程?这是一个人可以创建的最简单的服务,即使对我来说也不起作用


提前感谢:

尝试将您的URI模板更改为:

[WebGet(UriTemplate="/GetData", ResponseFormat=WebMessageFormat.Xml)]
更新

uri模板中的位可以是任何内容,因此可以是:

/GetMySuperComplicatedData 
并且仍然映射到GetData


有关更多信息,请阅读此处:

尝试将URI模板更改为:

[WebGet(UriTemplate="/GetData", ResponseFormat=WebMessageFormat.Xml)]
更新

uri模板中的位可以是任何内容,因此可以是:

/GetMySuperComplicatedData 
并且仍然映射到GetData


有关更多信息,请阅读此处:

试试看http://localhost:60503/HelloWorld,并向我们展示您的Global.asaxhttp://localhost:60503/HelloWorld,并向我们展示您的Global.asax

您没有在任何地方定义这应该是REST服务-您的web.config中也没有使用webHttpBinding的端点,您也没有在*.svc文件中指定使用WebServiceHostFactory

最简单的修复方法是将svc文件修复为:

<%@ ServiceHost Language="C#" Debug="true" 
      Service="WCFRestExample.HelloWorld" CodeBehind="HelloWorld.svc.cs"
       Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
有了这个,您的svc文件现在定义了它想要使用WebServiceHost(理解REST的主机)来托管您的服务


查看有关使用WCF的REST服务的精彩介绍。

您没有在任何地方定义这应该是REST服务-您的web.config中也没有使用webHttpBinding的端点,也没有在*.svc文件中指定使用WebServiceHostFactory

最简单的修复方法是将svc文件修复为:

<%@ ServiceHost Language="C#" Debug="true" 
      Service="WCFRestExample.HelloWorld" CodeBehind="HelloWorld.svc.cs"
       Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
有了这个,您的svc文件现在定义了它想要使用WebServiceHost(理解REST的主机)来托管您的服务


查看有关使用WCF的REST服务的精彩介绍。

这可能是您没有正确设置ServiceHostFactory的原因之一

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

这可能是您没有正确设置ServiceHostFactory的原因之一

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

我试过了。仍然得到404错误。项目中没有global.asax。我应该加一个吗?我应该写些什么呢?创建项目时是否使用了WCF 4.0项目模板?它不是标准的,但您可以下载它文件->新建项目->在线模板->WCF->WCF REST服务模板40CS。它将创建一个项目,为您完成许多常见配置,包括global.asax。看那里,很清楚,我试过了。仍然得到404错误。项目中没有global.asax。我应该加一个吗?我应该写些什么呢?创建项目时是否使用了WCF 4.0项目模板?它不是标准的,但您可以下载它文件->新建项目->在线模板->WCF->WCF REST服务模板40CS。它将创建一个项目,为您完成许多常见配置,包括global.asax。看一看,这很清楚。还有其他的wsdl吗?不,至少在默认情况下,据我所知。这显然不是高度定制的解决方案:@Tom Squires:wsdl用于SOAP服务,而不是基于Http的REST服务。看来你对这也不熟悉;显示svc文件的内容,您可能配置了不正确的ServiceFactory。@Int3:这还不够,您还需要提供ServiceFactory是否存在用于REST的wsdl?不,至少在默认情况下,据我所知。这显然不是高度定制的解决方案:@Tom Squires:wsdl用于SOAP服务,而不是基于Http的REST服务。看来你对这也不熟悉;显示svc文件的内容,您可能错误地配置了ServiceFactory。@Int3:这还不够,您还需要提供ServiceFactory是否尝试在getData的末尾添加一个尾随斜杠?那么请求和uri模板上的/GetData/是否尝试在GetData的末尾添加一个尾随斜杠?所以/GetData/在请求和uri模板上