Web APIs, such as Android and database interaction, are required in many cross-platform applications.
Create a Web API Project
Select the Web node under the template under the new Project under the Visual C # node, select the ASP. NET Web application under the Template list, and name it Chatapp.
Under new ASP . NET project, select Web API Click OK.
Adding a Model
Create a new model under the solution
//New model User namespace app_ chat.models{public class User {publi C string UID {get; set;} public string LoginName {get; set;} public string Name {get; set;} public string PWD {get; set;} public DateTime createtime {get; set;} }}
//New model Responseloginstate namespace app_chat.models{public class responseloginstate {public user user {get; set ; } public int State {get; set;} Public string msg {get; set;}
}}
//New model Requestlogin namespace app_chat.models{public class Requestlogin {public string LoginName {get; set;} Public string PWD {get; set;}}}
Adding a Controller
Create a new controller under solution
Select an empty template to
The following is the name of the controller to be added (controllers naming is canonical, you cannot change the controller part of the back or add characters at the back door, otherwise it will result in the last inaccessible to this controller like this controller is through/api/ chatapp/access, of course, the default is GET request mode)
namespace app_chat.controllers{public class Chatappcontroller:apicontroller {// < Summary>/// User Login //</summary>// <param name= "User" > User's login name-LoginName, password-pwd</param>/ /<returns> Login Success Returns the user's information, and state=1</returns>
PublicResponseloginstate Login (requestlogin user) {if(string. Isnullorwhitespace (user. LoginName))return NewResponseloginstate () {state = 0, msg ="parameter error, loginname not out!" "};if(string. Isnullorwhitespace (user. PWD))return NewResponseloginstate () {state = 0, msg ="parameter error, name not coming out!" "};#region using(var conn =NewSystem.Data.OracleClient.OracleConnection (oraclehelper.connstring)) {Conn. Open (); var command = conn. CreateCommand (); Command. Parameters.clear (); Command. Parameters.Add (NewOracleParameter (": LoginName", user. LoginName)); Command. Parameters.Add (NewOracleParameter (":p WD", user. PWD)); Command.commandtext ="SELECT * from App_chatuser where loginname=:loginname and pwd=:p WD"; var reader = command. ExecuteReader (); User Loginuser =NewUser ();Try{Reader. Read (); Loginuser. UID = reader["USERID"]. ToString (); Loginuser. LoginName = reader["LOGINNAME"]. ToString (); Loginuser. Name = reader["NAME"]. ToString (); Loginuser. PWD = reader["PWD"]. ToString (); Loginuser. Createtime = Convert.todatetime (reader["Createtime"]);return NewResponseloginstate () {state = 1, msg ="Success", user = Loginuser}; }Catch(Exception ex) {return NewResponseloginstate () {state = 0, msg = ex. Message, user =NULL}; } }#endregion} }}
By accessing Api/chatapp/login, you can access Webapi.
JavaScript to detect Webapi
<!DOCTYPE HTML><HTML xmlns= "http://www.w3.org/1999/xhtml"><Head><Meta http-equiv= "Content-type" content= "text/html; charset=utf-8"/> <title></title> <Script src= "Scripts/jquery-1.10.2.min.js"></Script><script>varUserfunctionLogin () {$.ajax ({URL:'/api/chatapp/login ', type:' POST ', DataType:' JSON ', data: {LoginName:' Zhangsan 'Pwd:' 123 '}, Success:function(data) {if(typeof(data)! =' object ') data = json.parse (data); user = data. Data.user; Alert (json.stringify (data)); } }); }</Script></Head><Body> <input type= "button" value= "Login" onclick= "Login ()" /></Body></HTML>
If not clear, you can refer to the following Web site to learn Http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
Simple logging in Visual Studio 2013 creating ASP. 2