ASP.NET交互Rest服务接口(Jquery的Get与Post方式)
一 rest服务创建
其中web客户端(clintweb)不对其它层的引用,只通过rest部署后的服务进行效互;
1:实体层(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);
}
}
}
此处要注意[datacontract],[datamember]在命名空间using system.runtime.serialization下面;
2:接口层(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:逻辑层(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="踏浪帅",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 "您好:" + name;
}
}
}
后面创建的客户端传参数要跟上面各个方法的参数相同,比如:name,model等
4:rest服务(restservice)
此处新建一个文本文件把它修改成.s格式,在其里面写入:
<%@ servicehost language="c#" debug="true" service="servicebll.userbll" %>
web.config文件内容:
<?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">
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<servicemetadata httpgetenabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<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>
上一篇: 能选一个女孩出来就不错了