gpt4 book ai didi

c# - 找不到 Restful Web 服务端点

转载 作者:太空狗 更新时间:2023-10-29 21:46:33 25 4
gpt4 key购买 nike

我正在尝试构建一个基本的 REST Web 服务,它只返回当前时间,每当我尝试调用我的服务时,我都会收到以下错误 http://localhost:PORT/TimeService/CurrentTime :

Endpoint not found. Please see the service help page for constructing valid requests to the service.

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

Service1.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();
}
}
}

全局.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)));
}
}
}

网络配置

<?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>

最佳答案

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

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

http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx

关于c# - 找不到 Restful Web 服务端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8331824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com