Добавление кодов в файл Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<behaviors >
<serviceBehaviors>
<behavior name="http">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="www">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<services>
<service behaviorConfiguration="http" name="REST.CRUD.Alternativ">
<endpoint address=""
binding="webHttpBinding"
behaviorConfiguration="www"
contract="REST.CRUD.IAlternativ"
bindingConfiguration="rest">
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding allowCookies="true" name="rest">
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
<security>
<requestFiltering>
<verbs>
<add verb="GET" allowed="true" />
<add verb="POST" allowed="true" />
<add verb="DELETE" allowed="true" />
<add verb="PUT" allowed="true" />
</verbs>
</requestFiltering>
</security>
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="KendoDB" connectionString="Data Source=(localDB)\v11.0;Initial Catalog=Kendo;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
PersonDTO
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace REST.CRUD
{
public class PersonDTO
{
public int ID { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
}
public class KendoContext : DbContext
{
public KendoContext():base("KendoDB"){}
public DbSet<PersonDTO> PersonDTOs { get; set; }
}
}
IAlternativ.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Services;
namespace REST.CRUD
{
[ServiceContract]
public interface IAlternativ
{
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
List<PersonDTO> GetAll();
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
PersonDTO Get(int ID, string f, string l);
[WebInvoke(Method = "DELETE",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
String Delete(int ID, string f, string l);
[WebInvoke(Method = "PUT",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
String Update(int ID, string f, string l);
}
}
Alternativ.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Script.Services;
using System.Web.Services;
namespace REST.CRUD
{
public class Alternativ : IAlternativ
{
public List<PersonDTO> GetAll()
{
var db = new KendoContext();
return db.PersonDTOs.Select(c => c).ToList();
}
public PersonDTO Get(int ID, string f, string l)
{
var db = new KendoContext();
return db.PersonDTOs.Where(c => c.ID == ID).Select(c => c).First();
}
public String Delete(int ID, string f, string l)
{
//Delete Code
return "OK";
}
public String Update(int ID, string f, string l)
{
//Update Code
return "OK";
}
}
}
[Построить и запустить проект WCF (F5)].
Добавить JQuery в DOM для теста (Вставить в консоль)
(function() {
if (! window.jQuery ) {
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = '//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js';
(document.getElementsByTagName('head')[0] ||
document.getElementsByTagName('body')[0]).appendChild(s);
}
}());
Добавление функций в консоль
function POST(){
$.ajax({
type: "POST", //GET,POST,PUT or DELETE verb
url: "/Alternativ.svc/GetAll", // Location of the service
data: '{"ID":"1"}', //Data to be sent to server
dataType: "json", // content type sent to server
contentType: "application/json; charset=utf-8", //Expected data format from server
processdata: false, //True or False
success: function (response) {
console.log(response);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
}
});
}
function GET(){
$.ajax({
type: "GET", //GET,POST,PUT or DELETE verb
url: "/Alternativ.svc/Get", // Location of the service
data: {ID:1}, //Data to be sent to server
dataType: "json", // content type sent to server
contentType: "application/json; charset=utf-8", //Expected data format from server
processdata: true, //True or False
success: function (response) {
console.log(response);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
}
});
}
function DELETE(){
$.ajax({
type: "DELETE", //GET,POST,PUT or DELETE verb
url: "/Alternativ.svc/Delete", // Location of the service
data: '{"ID":"1"}', //Data to be sent to server
dataType: "json", // content type sent to server
contentType: "application/json; charset=utf-8", //Expected data format from server
processdata: false, //True or False
success: function (response) {
console.log(response);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
}
});
}
function PUT(){
$.ajax({
type: "PUT", //GET,POST,PUT or DELETE verb
url: "/Alternativ.svc/Update", // Location of the service
data: '{"ID":"1"}', //Data to be sent to server
dataType: "json", // content type sent to server
contentType: "application/json; charset=utf-8", //Expected data format from server
processdata: false, //True or False
success: function (response) {
console.log(response);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
}
});
}
Функции вызова
DELETE();
PUT();
GET();
POST();