【发布时间】:2016-11-15 06:28:01
【问题描述】:
我已经创建了 WCF Rest 服务,该服务从客户端获取输入/参数并将其保存在数据库和知识中,并使用添加数据的表的身份。当我通过客户端使用服务时,它只不过是 web 应用程序将 url 制作为 web请求并获得正确的输出。但是当我通过浏览器消费时出现错误。 首先我是这样尝试的:-
http://localhost:Portnumber/Test.svc/ADD/Bodywash/Orange/50
之后像这样
tp://localhost:Portnumber/Test.svc/ADD?Name=Bodywash&CategoryName=Orange&Price=50
我收到错误'Entpoint not found'。如何通过浏览器使用休息服务?我能做到吗
// Code of service
-- Table
CREATE TABLE [dbo].[Product](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[CategoryName] [varchar](50) NULL,
[Price] [int] NULL
)
-- Sp
ALTER procedure [dbo].[proc_InsertProduct]
(
@id int out,
@Name nvarchar(50),
@CategoryName nvarchar(50),
@Price int
)
as insert into Product
(
Name,
CategoryName,
Price
)
values
(
@Name,
@CategoryName,
@Price
)
set @id = @@identity
return @id
-- Interface
public interface ITest
{
[WebInvoke(UriTemplate = "ADD/{Name}/{CategoryName}/{Price}", Method = "POST", ResponseFormat = WebMessageFormat.Json)]
int InsertProduct(string Name, string CategoryName, string Price);
}
-- class
public class Test : ITest
{
public int InsertProduct(string Name, string CategoryName, string Price)
{
string constr = ConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("proc_InsertProduct", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@CategoryName", CategoryName);
cmd.Parameters.AddWithValue("@Price", Price);
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Direction = ParameterDirection.Output;//Output parameter
cmd.ExecuteNonQuery();
con.Close();
int result = (int)(cmd.Parameters["@id"].Value);
return result;//returning id
}
}
// WebConfig
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<connectionStrings>
<add name="SampleConnectionString" connectionString="Data Source=xxx.xxx.x.x;Initial Catalog=sample;Persist Security Info=True;User ID=sa;Password=xxxx" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="RestApiTest.Test" behaviorConfiguration="serviceBehavior">
<endpoint address="" binding="webHttpBinding" contract="RestApiTest.ITest" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
【问题讨论】:
-
我能做到吗 - 是的,您可以通过浏览器访问 RESTful WCF 的 GET 端点。但要确定您的问题,您可能还需要提供 WCF 代码。您尝试使用的方法似乎是 POST 方法 - 如果该方法 (ADD) 是 POST 方法,那么您无法通过浏览器访问它。改为用户提琴手/邮递员
-
是的,我的请求是post。我正在将记录插入到表中。我将修改我的问题。
-
如前所述,如果方法是POST,请使用Fiddler/Postman,而不是从浏览器中点击
-
感谢@developer 的帮助。我保持所有代码相同,只是将方法名称从 post 更改为 get。我知道 get 用于选择和 post 以进行插入/更新。这里我将数据来自 url 插入我的表并返回我的表 id。在所有浏览器中都可以正常工作。这会被认为是有效的吗?或者我将进一步面临任何问题。因为客户端可能正在通过浏览器访问它。