This article will look at a simple example of how to create a Rest service interface and invoke it via jquery, mainly in two ways: Get and post, where the simple Type (sring) and the complex type will be submitted via post ( Custom implementation Usermodel) interacts with rest services;
One Rest service creation
Where the Web client (Clintweb) does not refer to other layers, but only through the services after rest deployment;
1: Solid Layer (Model)
using System.Runtime.Serialization;
namespace Model
{
[DataContract]
public class UserModel
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
public string PassWord { get; set; }
[DataMember]
public int Age { get; set; }
public override string ToString()
{
return string.Format("ID:{0};姓名: {1};年龄:{2};密码:{3}",ID, UserName, Age, PassWord);
}
}
}
Note here [Datacontract],[datamember] under the namespace using System.Runtime.Serialization;
2: Interface Layer (ISERVICEINTERFACE)
using System.ServiceModel.Web;
using System.ServiceModel;
using Model;
namespace IServiceInterface
{
[ServiceContract]
public interface IUser
{
[WebGet(UriTemplate = "/{ID}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
UserModel GetUserFromID(string ID);
[WebGet(UriTemplate = "All", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
List<UserModel> GetAllUser();
[WebInvoke(UriTemplate = "/User/UserName", Method = "POST", RequestFormat = WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
String GetUserName(string Name);
[WebInvoke(UriTemplate = "/User/Post", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string UpdateUser(UserModel model);
}
}
3: Logical Layer (SERVICEBLL)
using IServiceInterface;
using Model;
namespace ServiceBll
{
public class UserBll: IUser
{
public static List <UserModel> GetUserList ()
{
List <UserModel> list = new List <UserModel> ()
{
new UserModel () {ID = 1, UserName = "Talangshuai", PassWord = "123456", Age = 27},
new UserModel () {ID = 2, UserName = "wujunyang", PassWord = "345678", Age = 30},
new UserModel () {ID = 3, UserName = "cnblogs", PassWord = "987654", Age = 33}
};
return list;
}
public UserModel GetUserFromID (string ID)
{
UserModel item = GetUserList (). Where (a => a.ID == int.Parse (ID)). SingleOrDefault ();
if (item! = null)
{
return item;
}
else
{
return new UserModel ();
}
}
public List <UserModel> GetAllUser ()
{
return GetUserList ();
}
public string UpdateUser (UserModel model)
{
return model.ToString ();
}
public String GetUserName (string Name)
{
return "Hello:" + Name;
}
}
}
The client-side-pass parameters created later are the same as the parameters of each method above, such as: Name,model, etc.
4:rest Service (Restservice)
Here, create a new text file that modifies it to the. svc format, written in it:
<%@ ServiceHost language= "C #" debug= "true" service= "SERVICEBLL.USERBLL"%>
Web.config file Contents:
<? xml version = "1.0" encoding = "utf-8"?>
<configuration>
<system.web>
<compilation debug = "true" targetFramework = "4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name = "webHttp">
<webHttp helpEnabled = "true" />
</ behavior>
</ endpointBehaviors>
<serviceBehaviors>
<behavior name = "MapConfigBehavior">
<!-To avoid leaking metadata information, please set the following values to false and delete the metadata endpoint above before deployment->
<serviceMetadata httpGetEnabled = "true" />
<!-To receive failure exception details for debugging, set the following values to true. Set to false before deployment to avoid leaking exception information->
<serviceDebug includeExceptionDetailInFaults = "true" />
<dataContractSerializer maxItemsInObjectGraph = "2147483647" />
</ behavior>
</ serviceBehaviors>
</ behaviors>
<bindings>
<webHttpBinding>
<binding name = "webHttpBindConfig" receiveTimeout = "00:30:00" sendTimeout = "00:30:00" maxReceivedMessageSize = "104857600">
<readerQuotas maxStringContentLength = "2147483647" maxArrayLength = "2147483647" />
<security mode = "None"> </ security>
</ binding>
</ webHttpBinding>
</ bindings>
<services>
<service name = "ServiceBll.UserBll" behaviorConfiguration = "MapConfigBehavior">
<endpoint binding = "webHttpBinding" contract = "IServiceInterface.IUser" bindingConfiguration = "webHttpBindConfig" behaviorConfiguration = "webHttp" />
</ service>
</ services>
</system.serviceModel>
</ configuration>