在 IService1.cs 页面中:-
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Json/{id}")]
string myfun(string id);
在Service1.cs页面中:-
public string myfun(string id)
{
string ConnectString = "server=localhost;database=test_local;integrated security=SSPI";
string QueryString = "select * from tbl_af_register where id=" + id;
SqlConnection myConnection = new SqlConnection(ConnectString);
SqlDataAdapter da = new SqlDataAdapter(QueryString, myConnection);
DataSet ds = new DataSet();
da.Fill(ds, "TestName");
string i = ds.Tables[0].Rows[0]["name"].ToString();
return "your name is " + i;
}
在 Web.Config 文件中:-
在 -> 'system.serviceModel' 部分
<system.serviceModel>
<services>
<service name="WcfService2.Service1">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="webHttpBinding" contract="WcfService2.IService1" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
</service>
</services>
在 web.config 的“行为”部分:-
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
注意:- 这个名字“web”应该和-> behaviorConfiguration="web" 一样