gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:43:31 24 4
gpt4 key购买 nike

我正在尝试使用 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();
}
}
}
}

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

最佳答案

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

编辑:

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

<service name="WcfRestSample.IUserService1">

<service name="WcfRestSample.UserService1">

这样做的一个后果是该服务不再在 wcf 测试客户端中加载,也许这就是为什么它会造成困惑 - 有些人记录了使用 wcf 测试客户端的步骤,而其他人则记录了使用 Web 浏览器的步骤 但它是一个简单的修复方法,可以将其改回使用界面!

关于.net - REST 服务未在浏览器中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19933616/

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