我创建了WCF Rest服务,它从客户端获取input / para并将其保存在数据库和知识中,并添加数据的表的身份。当我使用服务throgh客户端时,只有web应用程序将url作为web请求并获得正确的输出。但是当我通过浏览器消费时出错。 首先我尝试这样: -
http://localhost:Portnumber/Test.svc/ADD/Bodywash/Orange/50
之后就像这样
HT
tp://localhost:Portnumber/Test.svc/ADD?Name=Bodywash&CategoryName=Orange&Price=50
我收到错误'找不到点'。如何通过浏览器消费休息服务?我能做到吗
// 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>
答案 0 :(得分:0)
继续发表评论 - 由于评论的大小限制而更新为答案
这会工作虽然我不推荐这样做 - 尊重HTTP动词,所有获取请求应该是GET,添加应该是POST,更新应该是PUT(补丁的情况下补丁)和删除应该是DELETE方法。我建议你将这个方法还原为POST。
因为客户端可能正在通过浏览器访问它 - 因此,我假设您的意思是说将使用JavaScript代码从客户端使用此服务。如果这是真的,那么你不必担心它。如果有人在您的REST服务中POST数据(例如,在jQuery的情况下使用$.ajax
或$.post
),您将获得正确的数据。
要使此方法成为标准POST方法,请更改方法签名以接受comples对象,而不是获取Name,CategoryName&amp;路线的价格。将此int InsertProduct(string Name, string CategoryName, string Price);
更改为int InsertProduct(Product product);
,其中&#39;产品&#39; class有必需的属性。您可以在此处找到示例实现 - POST complex object to WCF REST。