C# 如何从浏览器中仅调用RESTful端点?
我试图以编程方式创建端点,不想在配置文件中指定端点配置 我想添加RESTful端点,所以在添加RESTful端点之后,我应该能够从浏览器调用这些RESTful端点 添加端点后,我在方法上放置了一个调试器,但没有调用我的方法,也没有看到任何输出 我不明白我的代码有什么问题。根据我在以编程方式添加此配置时的理解,我不需要在配置文件中定义此配置 Wcf服务代码:C# 如何从浏览器中仅调用RESTful端点?,c#,wcf,wcf-rest,C#,Wcf,Wcf Rest,我试图以编程方式创建端点,不想在配置文件中指定端点配置 我想添加RESTful端点,所以在添加RESTful端点之后,我应该能够从浏览器调用这些RESTful端点 添加端点后,我在方法上放置了一个调试器,但没有调用我的方法,也没有看到任何输出 我不明白我的代码有什么问题。根据我在以编程方式添加此配置时的理解,我不需要在配置文件中定义此配置 Wcf服务代码: public interface ICalculator { [OperationContract]
public interface ICalculator
{
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Add/{n1}")]
string Add(string n1);
}
public class CalculatorService : ICalculator
{
public string Add(string n1)
{
return n1;
}
}
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
</httpModules>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
preCondition="managedHandler"/>
</modules>
<directoryBrowse enabled="true"/>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
</configuration>
公共接口计算器
{
[经营合同]
[WebInvoke(Method=“GET”,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,UriTemplate=“Add/{n1}”)]
字符串Add(字符串n1);
}
公共类计算器服务:ICalculator
{
公共字符串添加(字符串n1)
{
返回n1;
}
}
中的Uri应仅包括服务的基址
请尝试以下代码:
WebServiceHost serviceHost = new WebServiceHost(typeof(CalculatorService),
new Uri("http://localhost:56264"));
我试过了,但很抱歉,我仍然无法调用我的方法
WebServiceHost serviceHost = new WebServiceHost(typeof(CalculatorService),
new Uri("http://localhost:56264"));