MVC請求 處理 響應【使用者登陸】

來源:互聯網
上載者:User

標籤:mvc請求 處理 響應

MVC路由設定  App_Start/RouteConfig.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace Mvclogin{    public class RouteConfig    {        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                name: "Default",                url: "{controller}/{action}",                defaults: new { controller = "Login", action = "Index" }            );        }    }}


控制器 Controllers/LoginController.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Data;using System.Data.SqlClient;namespace Mvclogin.Controllers{    public class LoginController : Controller    {        //        // GET: /Login/        public ActionResult Index()        {            return View();        }        public ActionResult Add()        {            string Name = Request["UserName"].ToString();            string Pass = Request["password"].ToString();            string sql = "insert into T_Login values(@userName,@pass)";            int i = SqlHelper.ExecuteNonQuery(sql, new SqlParameter("userName", Name), new SqlParameter("pass", Pass));            return Content("OK");        }        public ActionResult Login()        {            return View("Login");        }        public ActionResult LoginResponse()        {            string Name = Request.Form["UserName"].ToString();            string Pass = Request.Form["password"].ToString();            string sql = "select * from T_Login where [email protected]";            DataTable dt = SqlHelper.ExecuteDataTable(sql, new SqlParameter("Name", Name));            if (dt.Rows.Count <= 0)            {                ViewData["Message"] = "使用者名稱不存在";                return View("Login");            }            if (dt.Rows.Count > 1)            {                ViewData["Message"] = "大事不好,查詢出多條資料";                return View("Login");            }            if (dt.Rows[0]["password"].ToString() != Pass)            {                ViewData["Message"] = "密碼錯誤啦";                return View("Login");            }                     return Content("恭喜你~登陸成功啦");        }    }}


視圖

Index視圖  Views/Login/Index.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><!DOCTYPE html><html><head runat="server">    <meta name="viewport" content="width=device-width" />    <title>使用者註冊</title></head><body>    <div>        <form method="post" action="/Login/Add">        <table>            <tr>              <tr><th>使用者名稱:</th><td><input type="text" name="UserName" /></td></tr>             <tr><th>密碼:</th><td><input type="password" name="password" /></td></tr>            <tr><td colspan="2" align="center"><input type="submit" value="註冊" /></td></tr>        </table>        </form>    </div></body></html>

Login視圖 Views/Login/Login.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><!DOCTYPE html><html><head runat="server">    <meta name="viewport" content="width=device-width" />    <title>Login</title></head><body>    <div>        <form method="post" action=/Login/LoginResponse>        <table>            <tr><th>使用者名稱:</th><td><input type="text" name="UserName" /></td></tr>             <tr><th>密碼:</th><td><input type="password" name="password" /></td></tr>             <tr><td colspan="2" align="center"> <input type="submit" value="登陸" /></td></tr>             <tr><td colspan="2" align="center"> <span><%: ViewData["Message"] %></span></td></tr>        </table>        </form>    </div></body></html>





MVC請求 處理 響應【使用者登陸】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.