码迷,mamicode.com
首页 > 其他好文 > 详细

WCF REST 工作总结

时间:2014-05-29 09:41:26      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   tar   

首先引用System.ServiceModel;System.ServiceModel;System.ServiceModel.Activation;命名空间

bubuko.com,布布扣
[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);
    }
View Code
bubuko.com,布布扣
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class APIForApp : IAPI
    {
View Code

上面的是接口和实现代码

global

bubuko.com,布布扣
protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("API", new WebServiceHostFactory(), typeof(WebAdmin.API.APIForApp)));
        }
View Code

webconfig

bubuko.com,布布扣
<!--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 配置结束-->
View Code

ajax调用

bubuko.com,布布扣
$(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);
                }
            });

        });
View Code

 注意method="动作",动作有POST,GET等,必须大写。

WCF REST 工作总结,布布扣,bubuko.com

WCF REST 工作总结

标签:style   c   class   blog   code   tar   

原文地址:http://www.cnblogs.com/w158357686/p/3756778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!