简体   繁体   English

在WCF REST服务上找不到404

[英]404 not found on WCF REST service

I'have configured REST on WCF service. 我已经在WCF服务上配置了REST。 I have an other project working like this and copied from to my project all config and things. 我有另一个像这样工作的项目,并将所有配置和内容从其复制到我的项目中。 But when I run it, rest method and /web page not found. 但是当我运行它时,找不到rest方法和/ web页面。

Web.config: Web.config:

> <appSettings>
>     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />   </appSettings>   <system.web>
>     <compilation debug="true" targetFramework="4.5" />
>     <httpRuntime targetFramework="4.5"/>   </system.web>   <system.serviceModel>
>     <services>
>       <service name="WcfRestTest.Service1">
>         <clear />
>         <endpoint behaviorConfiguration="RFEndPointBehavior" binding="webHttpBinding"
>           contract="WcfRestTest.IService1" listenUriMode="Explicit">
>         </endpoint>
>       </service>
>     </services>
>     <behaviors>
>       <endpointBehaviors>
>         <behavior name="RFEndPointBehavior"  >
>           <webHttp helpEnabled="true"/>
>         </behavior>
>       </endpointBehaviors>
>     </behaviors>
>     <protocolMapping>
>       <add binding="basicHttpsBinding" scheme="https" />
>     </protocolMapping>
>     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />   </system.serviceModel>  
> <system.webServer>
>     <modules runAllManagedModulesForAllRequests="true"/>
>     <directoryBrowse enabled="true"/>   

Service Class: 服务等级:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    public List<Student> GetStudentDetails()
    {
        List<Student> stuList = new List<Student>();
        stuList.Add(new Student { ID = "1", Name = "Test" });
        return stuList;
    }
}

Service Contract: 服务合同:

[ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "Students", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        List<Student> GetStudentDetails();
    }

[DataContract]
public class Student
{
    [DataMember]
    public string ID { get; set; }
    [DataMember]
    public string Name { get; set; }
}

I coped you code segments and found it works well. 我对您的代码段进行了处理,发现它很好用。 In my opinion, there is something wrong with the address you accessed. 我认为您访问的地址有问题。
For the WCF Rest service hosted in IIS, we are supposed to use the IIS base address and the UriTemplate property, 对于IIS中托管的WCF Rest服务,我们应该使用IIS基址和UriTemplate属性,

[WebGet(UriTemplate = "Students", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]

For example, here is a below site binding in IIS. 例如,这是IIS中的以下站点绑定。
在此处输入图片说明
The service base address is http://MyIP:11000/service1.svc , and the operation should be routed by using http://MyIP:11000/service1.svc/Students . 服务的基本地址为http:// MyIP:11000 / service1.svc ,并且应该使用http:// MyIP:11000 / service1.svc / Students路由操作。
One more thing must be noted is that the service usually has a relative address. 必须注意的另一件事是该服务通常具有一个相对地址。 Assumed that there is a below definition. 假设存在以下定义。

<endpoint address="myservice" binding="webHttpBinding" contract="WcfService3.IService1" listenUriMode="Explicit"></endpoint>

It should also be taken into account. 还应该考虑到它。
http://MyIP:11000/service1.svc/myservice/Students http:// MyIP:11000 / service1.svc / myservice / Students
Feel free to let me know if there is anything I can help with. 请随时告诉我是否有什么我可以帮助的。

Updated 更新

<system.serviceModel>
<services>
  <service name="WcfRestTest.Service1">
    <clear />
    <endpoint behaviorConfiguration="RFEndPointBehavior" binding="webHttpBinding"
      contract="WcfRestTest.IService1" listenUriMode="Explicit">
    </endpoint>
  <endpoint address="" binding="webHttpBinding" contract="WcfRestTest.IService1" behaviorConfiguration="RFEndPointBehavior" bindingConfiguration="mybinding"></endpoint>
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding name="mybinding">
      <security mode="Transport">
        <transport clientCredentialType="None"></transport>
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="RFEndPointBehavior"  >
      <webHttp helpEnabled="true"/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<protocolMapping>
  <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM