Asp.Net api接口

 1 using Entity;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 using System.IO;
 7 using Newtonsoft.Json;
 8 using BLL;
 9 
10 namespace WebProject.Web.api
11 {
12     /// <summary>
13     /// Login 用户登录
14     /// </summary>
15     public class Login : IHttpHandler
16     {
17         public void ProcessRequest(HttpContext context)
18         {
19             context.Response.ContentType = "application/json";
20             UserInfoEntity entity = null;
21             context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
22             using (var reader = new StreamReader(context.Request.InputStream))
23             {
24                 string postJson = reader.ReadToEnd();
25                 entity = (UserInfoEntity)JsonConvert.DeserializeObject(postJson, typeof(UserInfoEntity));
26             }
27             SetParameter parameter = new SetParameter();
28             if (context.Request.RequestType.ToLower() == "post")
29             {
30                 bool isSuccess = UserInfoBLL.getLogin(entity.Account, entity.Password);
31                 if (string.IsNullOrEmpty(entity.Account))
32                 {
33                     parameter.state = 0;
34                     parameter.msg = "请输入用户名";
35                     string strAccount = JsonConvert.SerializeObject(parameter);
36                     context.Response.Write(strAccount);
37                     return;
38                 }
39                 if (string.IsNullOrEmpty(entity.Password))
40                 {
41                     parameter.state = 0;
42                     parameter.msg = "请输入密码";
43                     string strPassword = JsonConvert.SerializeObject(parameter);
44                     context.Response.Write(strPassword);
45                     return;
46                 }
47                 parameter.Entity.account = entity.Account;
48                 parameter.Entity.password = entity.Password;
49                 if (parameter.Entity.password != entity.Password || parameter.Entity.account != entity.Account)
50                 {
51                     parameter.state = 0;
52                     parameter.msg = "用户名或密码错误";
53                     string strJson = JsonConvert.SerializeObject(parameter);
54                     context.Response.Write(strJson);
55                     return;
56                 }
57                 parameter.state = 1;
58                 parameter.msg = "登录成功";
59                 string strSuccess = JsonConvert.SerializeObject(parameter);
60                 context.Response.Write(strSuccess);
61             }
62         }
63         public bool IsReusable
64         {
65             get
66             {
67                 return false;
68             }
69         }
70         public class SetParameter
71         {
72             public int state;
73             public string msg;
74             public ParameterEntity Entity = new ParameterEntity();
75         }
76         public class ParameterEntity
77         {
78             public string account { get; set; }
79             public string password { get; set; }
80         }
81     }
82 }
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Entity
 8 {
 9     public class UserInfoEntity
10     {
11         private string account;
12 
13         public string Account
14         {
15             get { return account; }
16             set { account = value; }
17         }
18         private string password;
19 
20         public string Password
21         {
22             get { return password; }
23             set { password = value; }
24         }
25     }
26 }
原文地址:https://www.cnblogs.com/xiaoyao095/p/6955786.html