ASP.NET腳手架是ASP.NET Web應(yīng)用程序的代碼生成框架。 Visual Studio 2013包含用于MVC和Web API項(xiàng)目的預(yù)安裝代碼生成器。當(dāng)想快速添加與數(shù)據(jù)模型交互的代碼時(shí),可以將腳手架添加到項(xiàng)目中。使用腳手架可以減少在項(xiàng)目中開發(fā)標(biāo)準(zhǔn)數(shù)據(jù)操作的時(shí)間。
正如在前面章節(jié)教程中所看到的,我們已經(jīng)為Index
,Create
,Edit
操作創(chuàng)建了視圖,并且還需要更新操作方法。但ASP.Net MVC提供了一種使用腳手架創(chuàng)建所有這些視圖和操作方法的更簡單的方法。
我們來看一個(gè)簡單的例子。它創(chuàng)建一個(gè)包含模型類 - Employee
的相同示例,但是這次將使用腳手架。
第1步 - 打開Visual Studio,然后單擊:文件 -> 新建 -> 項(xiàng)目 菜單選項(xiàng)。一個(gè)新的項(xiàng)目對話框打開。
第2步 - 在左側(cè)窗格中,選擇:模板 -> Visual C# -> Web 。
第3步 - 在中間窗格中,選擇“ASP.NET Web應(yīng)用程序”。
第4步 - 在名稱字段中輸入項(xiàng)目名稱:MVCScaffolding ,然后單擊確定 繼續(xù)。
將看到下面的對話框,要求設(shè)置ASP.NET項(xiàng)目的初始內(nèi)容。
第5步 - 為了簡單起見,選擇:空 選項(xiàng),并在為以下項(xiàng)添加文件夾和核心引用’ 中選中MVC 復(fù)選框,然后單擊確定。
它將使用最少的預(yù)定義內(nèi)容創(chuàng)建一個(gè)基本的MVC項(xiàng)目。
項(xiàng)目由Visual Studio創(chuàng)建后,將在“解決方案資源管理器” 窗口中看到許多文件和文件夾。
第一步是安裝實(shí)體框架(Entity Framework)。右鍵單擊該項(xiàng)目,然后選擇:管理NuGet程序包 ,打開NuGet軟件包管理器 。它將打開“NuGet包管理器”,在搜索框中搜索Entity Framework。如下所示 -
選擇實(shí)體框架(Entity Framework),然后點(diǎn)擊“安裝(Install)” 按鈕。 它將打開預(yù)覽對話框。
點(diǎn)擊確定 繼續(xù)下一步 -
點(diǎn)擊“我接受”按鈕開始安裝,完成后如下 -
當(dāng)實(shí)體框架安裝成功后,可看到如上面的截圖所示的出窗口的消息。
要添加模型,請右鍵單擊解決方案資源管理器 中的Models 文件夾,然后選擇:添加 -> 類 ,會看到添加新項(xiàng)目 對話框。
在中間平移中選擇:類,并在名稱字段中輸入:Employee.cs。如下圖所示 -
使用以下代碼將一些屬性添加到Employee
類。
using System;
namespace MVCScaffolding.Models {
public class Employee{
public int ID { get; set; }
public string Name { get; set; }
public DateTime JoiningDate { get; set; }
public int Age { get; set; }
}
}
經(jīng)過上一步,我們已經(jīng)創(chuàng)建了一個(gè)Employee
模型,現(xiàn)在需要在這個(gè)模型類上添加另一個(gè)類,它將與實(shí)體框架(Entity Framework)進(jìn)行通信來檢索和保存數(shù)據(jù)。 以下是Employee.cs
文件中的完整代碼。
using System;
using System.Data.Entity;
namespace MVCScaffolding.Models{
public class Employee{
public int ID { get; set; }
public string Name { get; set; }
public DateTime JoiningDate { get; set; }
public int Age { get; set; }
}
public class EmpDBContext : DbContext{
public DbSet<Employee> Employees { get; set; }
}
}
如上面所看到的EmpDBContext
是從一個(gè)名為DbContext
的EF
類派生的。 在這個(gè)類中,有一個(gè)名為DbSet
的屬性,它基本上代表了要查詢和保存的實(shí)體。
要添加腳手架,請右鍵單擊解決方案管理器中的Controllers
文件夾,然后選擇:添加 -> 新建搭建基架項(xiàng)目 ,如下圖所示 -
它將顯示“添加腳手架”對話框 -
選擇:包含視圖MVC 5控制器(使用Entity Framework) 項(xiàng),然后單擊“添加” 按鈕,這將顯示添加控制器對話框。
從模型類 下拉列表中選擇:Employee
,并從數(shù)據(jù)上下文類 下拉列表中選擇EmpDBContext。還將看到默認(rèn)情況下選擇了控制器名稱。
單擊“添加”按鈕繼續(xù),在由Visual Studio使用腳手架創(chuàng)建的EmployeesController
中看到以下代碼。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MVCScaffolding.Models;
namespace MVCScaffolding.Controllers
{
public class EmployeesController : Controller
{
private EmpDBContext db = new EmpDBContext();
// GET: Employees
public ActionResult Index()
{
return View(db.Employees.ToList());
}
// GET: Employees/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
return View(employee);
}
// GET: Employees/Create
public ActionResult Create()
{
return View();
}
// POST: Employees/Create
// 為了防止“過多發(fā)布”攻擊,請啟用要綁定到的特定屬性,有關(guān)
// 詳細(xì)信息,請參閱 http://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,JoiningDate,Age")] Employee employee)
{
if (ModelState.IsValid)
{
db.Employees.Add(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
// GET: Employees/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
return View(employee);
}
// POST: Employees/Edit/5
// 為了防止“過多發(fā)布”攻擊,請啟用要綁定到的特定屬性,有關(guān)
// 詳細(xì)信息,請參閱 http://go.microsoft.com/fwlink/?LinkId=317598。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name,JoiningDate,Age")] Employee employee)
{
if (ModelState.IsValid)
{
db.Entry(employee).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
// GET: Employees/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return HttpNotFound();
}
return View(employee);
}
// POST: Employees/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Employee employee = db.Employees.Find(id);
db.Employees.Remove(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
運(yùn)行應(yīng)用程序并使用瀏覽器打開URL: http://localhost:59083/Employees
,將看到以下輸出結(jié)果 -
可以看到視圖中并沒有數(shù)據(jù),因?yàn)槲覀冞€沒有向 Visual Studio 創(chuàng)建的數(shù)據(jù)庫添加任何記錄。
下面點(diǎn)擊添加(Create New)鏈接來一個(gè)記錄,它將顯示創(chuàng)建視圖,填入相關(guān)信息 -
點(diǎn)擊“創(chuàng)建”按鈕,它將更新Index 視圖,看到有一條記錄。如下所示-
也可以看到新記錄也被添加到數(shù)據(jù)庫中。
通過上面所看實(shí)現(xiàn)步驟可以看到,我們已經(jīng)通過使用腳手架實(shí)現(xiàn)了一個(gè)簡單的例子,這是一個(gè)用來創(chuàng)建模型類,視圖和動作方法的更容易方式。