REST服务未在浏览器中显示结果

时间:2013-11-12 15:47:35

标签: .net wcf web-services rest web-config

我正在尝试使用Rest为大学项目创建自己的Web服务。 在做了一些教程后,我已经能够创建自己的教程了,服务正在运行,并使用wcf测试客户端在visual studio中返回结果。

当我浏览该服务时(http://localhost:53215/UserService1.svc)我可以看到服务页面但http://localhost:53215/UserService1.svc/GetUsersNames发送给我404,找不到页面错误!

谁能看到我做错了什么?

这是我的代码。

的Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>    
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
    </compilation>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfRestSample.IUserService1">
        <endpoint address="" contract="WcfRestSample.IUserService1" binding="webHttpBinding" behaviorConfiguration="restBehavior"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <connectionStrings>
      <add name="cs4_databaseEntities" connectionString="metadata=res://*/cs4_model.csdl|res://*/cs4_model.ssdl|res://*/cs4_model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\cs4_database.mdf;integrated security=True;user instance=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

IUserService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfRestSample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IUserService1" in both code and config file together.
    [ServiceContract]
    public interface IUserService1
    {
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Xml)]
        List<string> GetUsersNames();
    }
}

UserService1.svc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfRestSample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "UserService1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select UserService1.svc or UserService1.svc.cs at the Solution Explorer and start debugging.
    public class UserService1 : IUserService1
    {
        public List<string> GetUsersNames()
        {
            using (cs4_databaseEntities entities = new cs4_databaseEntities())
            {
                return entities.Users.Select(user => user.Name).ToList();
            }
        }
    }
}

我使用的教程在浏览器中运行良好!

2 个答案:

答案 0 :(得分:4)

您的终端是 http://localhost:53215/UserService1.svc/rest/GetUsersNames。 “休息”部分来自您的配置'address =“rest”'

<强>编辑:

服务名称设置为接口而不是实现类,更改:

<service name="WcfRestSample.IUserService1">

<service name="WcfRestSample.UserService1">

这样做的一个结果是服务不再在wcf测试客户端中加载,也许这就是为什么它成为混乱的原因 - 有些人记录了他们使用wcf测试客户端和其他人用于Web浏览器的步骤但它很容易修复,使其恢复使用界面!

答案 1 :(得分:1)

<service name="WcfRestSample.IUserService1">更改为<service name="WcfRestSample.UserService1">。感谢用户-Marvin Smitt的评论