Step One: Demand analysis
My goal is to use the MVC framework for simple login verification. Enter the user name and password from the client. It is then passed to database validation. If the database exists with this user name ID and password, the success prompt for the client account name is returned. Otherwise, the client failure message is returned.
Step two: Build an MVC framework
New Project >web>web Application
Step three: First add a controller, then the Views folder to find the corresponding and Controller folder with the same name to build the view interface style, and finally modify the route to be able to access the browser
Controller:
public class Logincontroller:controller
{
Get:login
Public ActionResult Index ()
{
Return View ("LoginView");
}
VIEW:
<! DOCTYPE html>
<meta name= "viewport" content= "Width=device-width"/>
<title> Landing Interface </title>
<body>
<form method= "POST" action= "/login/login" > //action Submit and return to controller processing at this point you can first use # instead
User name: <input name= "LoginId" type= "text"/> <br/>
Password: <input name= "loginpwd" type= "password"/> <br/>
<input type= "Submit" value= "Login"/><br/>
@ViewData ["Info"] //browser Gets the contents of the controller
</form>
</body>
Routing RouteConfig.cs
Namespace Mvclogin
{
public class Routeconfig
{
public static void RegisterRoutes (RouteCollection routes)
{
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");
Routes. MapRoute (
Name: "Default",
URL: "{controller}/{action}/{id}",
defaults:new {controller = "Login", action = "Index", id = urlparameter.optional}
);
}
}
}
Step four: Browser access no problem, formally began to write content, first write the Models model section. Typically models contains entity classes, universal data access classes, Universal Database access classes (ADO)
1. The entity class is written. Write the user Login entity class. Corresponds to a database field.
Namespace Mvclogin.models
{
<summary>
Login entity class, properties: Login ID, login password, user name
</summary>
public class SysAdmin
{
public int LoginId {get; set;}
Public String loginpwd {get; set;}
Public String adminname{get; set;}
}
}
2. Universal database access class, where only the method of writing queries
Namespace Mvclogin.models
{
<summary>
Generic Data access Classes
</summary>
public class SQLHelper
{
//Define database connection string
public static string connstring = configurationmanager.connectionstrings["ConnString"]. ToString ();//Writes the database connection string in Web. config, which is called through the connectionstrings property of the ConfigurationManager class
public static SqlDataReader Getreader (String sql)
{
SqlConnection conn = new SqlConnection (connstring);
SqlCommand cmd = new SqlCommand (SQL, conn);
Try
{
Conn. Open ();
return CMD. ExecuteReader (commandbehavior.closeconnection);
}
catch (Exception ex)
{
Throw ex;
}
}
}
Web. config plus node
<connectionStrings>
<add name= "connstring" connectionstring= "server=.; Database=smdbweb; Uid=sa; Pwd=abc.123 "/>
</connectionStrings>
3. Write Entity access classes. The name of the entity Access class is generally the entity class name +service. Concrete action entities, encapsulating entities, returning entity objects
Namespace Mvclogin.models
{
<summary>
Data access Classes
</summary>
public class Sysadminservice
{
<summary>
Login based on login ID and password
</summary>
<param name= "Objsysadmin" ></param>
<returns></returns>
Public sysadmin adminlogin (sysadmin objsysadmin)
{
String sql = "Select AdminName from dbo. Admins WHERE loginid={0} and Loginpwd= ' {1} ';
Sql=string. Format (SQL, Objsysadmin.loginid, objsysadmin.loginpwd);
SqlDataReader SDR = sqlhelper.getreader (SQL);
if (SDR). Read ())
{
Objsysadmin.adminname = sdr["AdminName"]. ToString (); Returns the user name if the username ID and password are correct
}
Else
{
Objsysadmin = null;//Empty the object if username ID and password are incorrect
}
Sdr. Close ();
return objsysadmin;
}
}
}
Step Five: Write the controller. The controller typically consists of three steps. 1. Obtaining data 2. Business Process 3. Return data
Namespace Mvclogin.controllers
{
public class Logincontroller:controller
{
Get:login
Public ActionResult Index ()
{
Return View ("LoginView");
}
Public ActionResult Login ()
{
//[1] Get data
SysAdmin objsysadmin = new sysadmin
{
LoginId = Convert.ToInt32 (request["LoginId"]),
Loginpwd = request["Loginpwd"]. ToString ()
};
//[2] Business processing
Objsysadmin=new Sysadminservice (). Adminlogin (objsysadmin);
if (objsysadmin!=null)
{
viewdata["info"] = "welcome," + Objsysadmin.adminname; Transferring data using Viewdate
}
Else
{
viewdata["info"] = "User name or password error";
}
//[3] Return data
Return View ("LoginView");
}
}
}
C # uses the MVC framework for login verification