1、MVC原理圖:
2、MVC項目文件夾說明
MVC項目文件夾說明 1)、App_Data:用來保存數據文件,比如XML文件等 2)、App_Start:包含MVC啓動的相關類 RouteConfig.cs 路由配置 3)、Global.asax.cs全局應用程序控制文件 啓動路由配置RouteConfig.cs 4)、Controller:存放整個項目“控制器”代碼文件 5)、Models:存放整個項目的“模型”代碼文件 6)、Views:存放整個項目的“視圖”代碼文件(網頁/界面) |
|
重要文件說明 1)、Views下面的web.config:是整個視圖的配置文件;根目錄下的web.config:作用於整個項目的配置 2)、Global.asax.cs全局應用程序控制文件 |
3、MVC中的約定:
約定勝於配置 1)、提前規定好 2)、無需配置 3)、不遵守規則則出錯 |
|
1)、控制器:必須以Controller結尾 2)、視圖:必須放在Views文件夾下,並且要和控制器同名的子目錄中創建 |
|
視圖訪問和尋址規則 1)、在控制器中使用View()方法調用視圖,返回和“動作方法同名”的視圖 2)、尋址規則:View()方法默認從“View文件夾”下尋找和控制器同名的文件夾 |
4、控制器和動作方法任務分析
using System; using System.Collections.Generic; using System.Linq; using System.Web;//ASP.NET核心命名空間 using System.Web.Mvc;//MVC框架基本的命名空間 namespace MyFirstMVC.Controllers { public class HomeController : Controller//每個控制器必須繼承的基類 { // GET: Home //控制器的動作方法,用於響應客戶端的請求,並調用響應的視圖向瀏覽器輸出信息 public ActionResult Index() { //[1]獲取數據:接收請求數據 //[2]業務處理:和Model交互... //[3]返回視圖或跳轉到其他控制器 return View();//該方法默認調用Index視圖(與Index()方法同名的視圖) } } } |
|
.NET MVC框架小白入門創建項目步驟(詳細)
一、如圖:新建項目選擇如圖所示點確定。
三、此時項目如下圖:
四、右鍵Controllers文件夾-->添加-->控制器,出現下圖:
五、這裏一定要寫HomeController。點確定
六、右鍵如圖Index()-->添加視圖出現下圖:
七、按如圖所示,修改頁面
八、可以運行了。如圖:
VS2017如何新建一個MVC框架的簡單實例(計算器)
一、需求分析實現效果
二、實現步驟
步驟一:新建項目--->Web---->空模板
步驟二:添加控制器
1)、控制器:必須以Controller結尾 2)、視圖:必須放在Views文件夾下,並且要和控制器同名的子目錄中創建 |
步驟三:根據控制器名稱添加視圖
步驟四:添加Models模型 編寫具體的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class Calculator
{
public int getAVG(int sumScore, int sumSubject)
{
return sumScore / sumSubject;
}
}
}
步驟五:編寫視圖,具體展示的內容
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>CalculatorView</title>
</head>
<body>
<form method="post" action="/Calculator/Calculate">
計算平均成績<br />
總成績:<input type="text" name="sumScore" /> <br />
總科目:<input type="text" name="sumSubject" /><br />
<input type="submit" value="開始計算" /><br />
@ViewData["avgScore"]
</form>
</body>
</html>
步驟六:編寫控制器活動方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class CalculatorController : Controller
{
// GET: Calculator
public ActionResult Index()
{
//return View();
return View("CalculatorView");
}
public ActionResult Calculate()
{
//[1]獲取數據
int sumScore = Convert.ToInt32(Request.Params["sumScore"]);
int sumSubject = Convert.ToInt32(Request.Params["sumSubject"]);
//[2]處理數據
Calculator objCal = new Calculator();
int avgScore = objCal.getAVG(sumScore, sumSubject);
//[3]返回數據
ViewData["avgScore"] = "平均成績爲:" + avgScore;
return View("CalculatorView");
}
}
}
步驟七:修改路由( App_Start:包含MVC啓動的相關類 RouteConfig.cs 路由配置)
using System; namespace WebApplication1 routes.MapRoute( |
using System; namespace WebApplication1 routes.MapRoute( |
這樣,MVC的三層就簡單的連接起來了~大功告成!
ASP.NET MVC三層架構基礎詳細操作圖文教程(VS2017)(2019.07.27)
講到這裏,我們已經把BLL和DAL都封裝好了。接下來的就只是調用。因爲本文主要是講述的ASP.NET MVC的三層架構,所以從最底層的DAL到BLL到現在UI反着來的,如果是現實中的項目咱們就不能這麼做了。得先做需求,然後做設計,然後搭建框架針對不同的模塊,進行不同的封裝。先UI,再BLL,最後DAL,因爲總是邏輯後纔會知道需要對數據怎麼操作。但在這之前都是用戶的體驗優先,因爲展示給用戶使用,操作和體驗的UI層可能會引導你的代碼邏輯。好啦,今天我們主要是講下在UI層下,我們怎麼與BLL層通迅。
首先,我們來說說要要在界面上實現的一些效果。
第一,實現用戶註冊,用戶需要輸入用戶名和密碼。然後註冊 。
第二,註冊成功後用戶可以登錄系統。
第三,登錄系統後用戶可以查看用戶列表。
新增一個MVC5視圖頁login
我們要在web項目中去調用BLL和Entity的信息我們就必須對它們添加引用。添加引用說過很多次了就不重複了。
然後我們就可以調用BLL中封裝的方法。
具本Login.cshtml頁面代碼如下:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<title></title>
</head>
<body>
@using (Html.BeginForm("Register", "home"))
{
<div>
<div><a>用戶註冊:</a></div>
<div><a>用戶賬號:</a><input type="text" name="usernameR" placeholder="賬號" /></div>
<div><a>用戶密碼:</a><input type="password" name="userpasswordR" placeholder="密碼" /></div>
<div><input type="submit" value="註冊用戶信息" /></div>
<div>@Html.ValidationMessage("infoR")</div>
</div>
<br />
}
@using (Html.BeginForm("login", "home"))
{
<div>
<div><a>用戶登錄:</a></div>
<div><a>用戶賬號:</a><input type="text" name="usernameL" placeholder="賬號" required /></div>
<div><a>用戶密碼:</a><input type="password" name="userpasswordL" placeholder="密碼" required /></div>
<div><input type="submit" value="用戶登錄" /></div>
<div>@Html.ValidationMessage("infoL")</div>
</div>
<br />
}
<div>
<table border="1">
<tr><th>用戶索引</th><th>用戶賬號</th><th>用戶密碼</th></tr>
@{
IEnumerable<StudyCSharp.Entity.UserInformation> ieeee = StudyCSharp.BLL.Userinformation_BLL.GetAllUserInfo();
foreach (var item in ieeee)
{
<tr>
<td>@item.ID</td>
<td>@item.UserName</td>
<td>@item.UserPassword</td>
</tr>
}
}
</table>
</div>
</body>
</html>
同時爲LoginController內添加想要的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Controllers
{
public class LoginController : Controller
{
// GET: Login
public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
ViewBag.Message = "Your login page.";
return View();
}
[HttpPost]
public ActionResult Login(string usernameL, string userpasswordL)
{
if (!ModelState.IsValid)
{
return View();
}
else
{
if (StudyCSharp.BLL.Userinformation_BLL.UserLogin(usernameL, userpasswordL))
{
ModelState.AddModelError("infoL", "登錄成功");
return View("index");
}
else
{
ModelState.AddModelError("infoL", "用戶名或密碼錯誤");
return View("login");
}
}
}
[HttpPost]
public ActionResult Register(string usernameR, string userpasswordR)
{
if (!ModelState.IsValid)
{
return View("login");
}
else
{
StudyCSharp.Entity.UserInformation user = new Entity.UserInformation();
user.ID = 0;
user.UserName = usernameR;
user.UserPassword = userpasswordR;
if (StudyCSharp.BLL.Userinformation_BLL.CreateUserInfo(user) > 0)
{
ModelState.AddModelError("infoR", "註冊成功");
return View("login");
}
else
{
ModelState.AddModelError("infoR", "註冊失敗");
return View("login");
}
}
}
}
}
下圖爲最終效果:
ASP MVC 後臺控制器彈出框提示
提示框點擊確定後頁面變成空白頁面
return Content("<script>alert('請先登錄');</script>");
提示框點擊確定後頁面變成你想要的頁面(如實原頁面怎會刷新一下)
return Content("<script>alert('請先登錄');window.location.href='../home/index';</script>");
接下來纔是我所要講的重點:提示框點擊確定後頁面返回原頁面(不刷新,還保留頁面用戶輸入的數據),其實它的實質爲後退一步歷史記錄。
return Content("<script>alert('請先登錄');history.go(-1);</script>");