Simple logging in Visual Studio 2013 creating ASP. 2

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.