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 UserName=@Name";
            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>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值