刚开始做ASP.NET应用程序的时候,自己一脸蒙B,具体的程序流程都不懂,所以自己打算写一个最简单的项目来看看ASP.NET MVC项目的具体流程。
若有写得不好的,还望指出.
目录结构如下图所示:
在企业中开发的时候需要自建Area(领域),所以我把最初项目带有的MVC文件夹我都删除了。
Controllers(控制层) UserController.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication4.Areas.LZX.Models;
namespace WebApplication4.Areas.LZX.Controllers
{
public class UserController : Controller
{
//
// GET: /LZX/User/
public ActionResult Index()
{
return View();
}
//操作名(方法名)必須与Views/User 下的cshtml文件的名字一致,当return View(),就会跳转到相对应的视图
public ActionResult LoginSuccess()
{
return View();
}
[HttpPost]
public ActionResult Login(String UserName, String Password)
{
User user = new User();
user.UserName = UserName;
user.Password = Password;
//可以写具体的逻辑代码,比如登录验证....
return Json(user);
}
}
}
//可以写具体的逻辑代码,比如登录验证....
return Json(user);
}
}
}
Models(模型层) User.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication4.Areas.LZX.Models
{
public class User
{
public String UserName { get; set; }
public String Password { get; set; }
}
}
Views(视图层) User/Index.cshtml代码:
@{
ViewBag.Title = "Index";
}
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<h2>登錄界面:</h2>
<form method="post">
帳號:<input type="text" id="username" name="username" /><br /><br />
密碼:<input type="password" id="password" name="password" /><br /><br />
<input type="button" id="submit" value="登錄" /><br />
</form>
<script>
$(function () {
$("#submit").click(function () {
var UserName = $("#username").val();
var Password = $("#password").val();
$.ajax({
type: "post",
url:"/LZX/User/Login",
data: { UserName: UserName, Password: Password },
dataType:"json",
async:false,
success:function(data){
alert(data.UserName);
window.location.href = "/LZX/User/LoginSuccess?username="+data.UserName;
}
});
});
});
</script>
Views(视图层) User/LoginSuccess.cshtml代码:
@{
ViewBag.Title = "LoginSuccess";
}
<h2>@Request["username"] LoginSuccess 登录成功。。。。</h2>
运行结果:
項目源码下載地址(代码会有点小区别,同样的原理):https://download.csdn.net/download/laizhixue/11010861