Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 找不到Restful web服务终结点_C#_Asp.net_Wcf_Rest - Fatal编程技术网

C# 找不到Restful web服务终结点

C# 找不到Restful web服务终结点,c#,asp.net,wcf,rest,C#,Asp.net,Wcf,Rest,我正在尝试构建一个基本的REST web服务,该服务只返回当前时间,每当我尝试调用我的服务时,就会出现以下错误: 找不到终结点。请参阅服务帮助页以了解构造 对服务的有效请求 我做错了什么?下面你可以找到我正在使用的所有代码。多谢各位 服务1.cs using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Activa

我正在尝试构建一个基本的REST web服务,该服务只返回当前时间,每当我尝试调用我的服务时,就会出现以下错误:

找不到终结点。请参阅服务帮助页以了解构造 对服务的有效请求

我做错了什么?下面你可以找到我正在使用的所有代码。多谢各位

服务1.cs

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

namespace WcfRestService1
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class TimeService
    {
        [WebGet(UriTemplate = "CurrentTime")]
        public string CurrentTime()
        {
            return DateTime.Now.ToString();
        }
    }
}
Global.asax

using System;
using System.ServiceModel.Activation;
using System.Web;
using System.Web.Routing;

namespace WcfRestService1
{
    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();
        }

        private static void RegisterRoutes()
        {
            RouteTable.Routes.Add(new ServiceRoute("TimeService",
                new WebServiceHostFactory(), typeof(TimeService)));
        }
    }
}
web.config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

</configuration>

这是我第一次开始学习rest时发现的最好的文章之一;我希望我现在正在工作,我有确切的TimeService示例,但我只是在谷歌上搜索,找不到相同的代码

明天下班后,如果您仍然需要,我将发布一个工作示例


我不知道这是否会导致执行选项,但您是否缺少CurrentTime Method上的
[OperationContract]
属性。自.NET 4.0以来,如果您有[WebGet]或[WebInvoke]用于WCF HTTP服务,则不再需要[OperationContract]。全局.asax位于哪个vdir/application?如果它不是直接在IIS根目录下,那么地址应该是@carlosfigueira。我只是通过Visual studio调试它来运行它。当它加载浏览器时,您可以通过它访问它,它只会显示解决方案的所有文件。尝试更改为匿名端口。如果您可以发布,这将非常有用。