首页 > 代码库 > WCF REST 工作总结
WCF REST 工作总结
首先引用System.ServiceModel;System.ServiceModel;System.ServiceModel.Activation;命名空间
[ServiceContract] public interface IAPI { /// <summary> /// 登录 /// </summary> /// <param name="UserName">用户名</param> /// <param name="Password">密码</param> /// <returns></returns> [OperationContract(Name = "Login")] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] GeneralResult Login(string UserName, string Password); }
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class APIForApp : IAPI {
上面的是接口和实现代码
global
protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(WebAdmin.API.APIForApp))); }
webconfig
<!--wcf rest 配置开始--> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> </system.webServer> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> <standardEndpoints> <webHttpEndpoint> <!--编写REST的时候使用的配置--> <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/> <!--helpEnabled 是否开启帮助 automaticFormatSelectionEnabled 是否启动自动格式--> </webHttpEndpoint> </standardEndpoints> </system.serviceModel> <!--wcf rest 配置结束-->
ajax调用
$(function () { $.ajax({ type: "POST", contentType: "application/json", data: ‘{ "UserName": "111","Password":"111" }‘, //比如 url: "http://localhost:9800/API/Login", dataType: ‘json‘, error: function (x, e) { console.log(x); }, success: function (response) { console.log(response); } }); });
注意method="动作",动作有POST,GET等,必须大写。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。