阻止控制器中方法被公開為Action方法
??在控制器類中,默認會將所有公共Action方法視為操作方法,但在某些特殊情況下,不希望被作為Action方法公開。就需要在方法上加上NonAction特性禁止公開,也就不能通過URL訪問了。
重命名Action方法
??MVC框架默認指定Action方法名與成員方法名一致,但如果控制器類成員方法上應(yīng)用了ActionName特性并指定了另一個名稱,那就是重命名了,與Action方法相對應(yīng)的視圖文件也要使用指定的名稱。
- 本例中,Action名稱GetItems重命名為get-items。
public class HomeController : Controller
{
[ActionName("get-items")]
public ActionResult GetItems()
{
return View();
}
}
使用布局頁
??布局頁可作為項目內(nèi)各視圖母版,用于排版被重復(fù)使用的內(nèi)容。
??在布局頁中,可在內(nèi)容頁出現(xiàn)位置用RenderBody方法來生成占位符,內(nèi)容頁HTML元素將會填滿。
??在內(nèi)容頁中,通過Layout屬性可以設(shè)置布局頁名稱。如果布局頁位于可被查找位置,就不需要指定路徑和擴展名,否則就要明確指定。
??一般在命名有特殊用途的視圖文件時都會以下劃線開頭,所以布局頁通常命名為_Layout.cshtml。
- 例
??在View目錄下建Shared目錄,Shared目錄下添加布局頁_Layout.cshtml。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
<div style="height:100px; background-color:slateblue;position:relative">
<p style="color:white;font-size:40px;position:absolute;margin-left:15px">
清新小站
</p>
</div>
<div style="margin-top:35px;margin-bottom:45px">
@RenderBody()
</div>
<div>
<hr/>
? 2018 - 2018 版權(quán)所有
</div>
</body>
</html>
??在View目錄下建Test目錄,Test目錄下添加布局頁Default.cshtml。
@{
Layout = "_Layout";
}
<div>
網(wǎng)站主頁
</div>
??添加控制器
public class TestController : Controller
{
public ActionResult Default()
{
return View();
}
}
_ViewStart視圖與_ViewImports視圖
??_ViewStart視圖與_ViewImports視圖不用于定義可視化內(nèi)容,而是用于聲明一些在視圖中重復(fù)使用的指令。
??_ViewImports視圖專門用于導(dǎo)入命名空間,System、System.Threading.Tasks、Microsoft.AspNetCore.Mvc等,這樣不需要每個視圖文件都寫一遍using指令。
??_ViewStart視圖用來放置各個視圖中都可能重復(fù)使用的指令,例如引用頁_Layout,后面就不用重復(fù)寫了,在執(zhí)行每個視圖文件時都會先執(zhí)行_ViewStart視圖中的代碼。
??_ViewStart與_ViewImports并不要求放置到/View/Shared目錄下,它們可以放到任何存有視圖文件的目錄下,只對當前目錄及子目錄下的視圖起作用。
- 例
??定義Demo控制器,對應(yīng)五個視圖。
public class DemoController : Controller
{
public ActionResult Index() => View();
public ActionResult Desc() => View();
public ActionResult News() => View();
public ActionResult Products() => View();
public ActionResult ContactUs() => View();
}
??在View目錄下建Shared子目錄,并在Shared中存放_Layout。
??nav標簽所定義的導(dǎo)航欄包括指向五個視圖的鏈接,此處可使用 asp-controller 和 asp-action 兩個標簽幫助器使框架自動生成導(dǎo)航的URL。
@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
<nav>
<a asp-controller="Demo" asp-action="Index">主頁</a>
<a asp-controller="Demo" asp-action="Desc">公司簡介</a>
<a asp-controller="Demo" asp-action="News">新聞</a>
<a asp-controller="Demo" asp-action="Products">產(chǎn)品信息</a>
<a asp-controller="Demo" asp-action="ContactUs">聯(lián)系我們</a>
</nav>
<div>
@RenderBody()
</div>
</body>
</html>
??在View目錄下建Demo子目錄對應(yīng)控制器,在建對應(yīng)的五個視圖(內(nèi)容為此形式<h3>主頁</h3>)。
??在Demo目錄下添加 _ViewStart 視圖文件,導(dǎo)入可能會被用到的命名空間。
@{
Layout = "_Layout";
}
@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
??在Demo目錄下添加 _ViewImports 視圖文件,寫入會被重復(fù)使用的代碼。
@using System
@using Microsoft.AspNetCore.Mvc
@using Microsoft.AspNetCore.Mvc.TagHelpers
在控制器中接收服務(wù)列表的注入
??服務(wù)類型有時使用“接口-實現(xiàn)類”的方式來開發(fā),因此一個服務(wù)接口可能有多個實現(xiàn)類。
services.AddScoped<ITestService, MD5CrtTest>();
services.AddScoped<ITestService, SHA256Test>();
??一種方法時以接口實現(xiàn)類依賴注入。
public DemoController(MD5CrtTest svs1, SHA256Test svs2)
{
_svs1 = svs1;
_svs2 = svs2;
}
??另一種方法是用IEnumerable<T>接口一同依賴注入。
public DemoController(IEnumerable<ITestService> svs)
{
_encoders = svs;
}
使用IFormCollection組件來提取form表單數(shù)據(jù)
??可以通過<form>元素收集用戶輸入的內(nèi)容,提交服務(wù)器后轉(zhuǎn)由指定控制器中的某個操作方法進行處理。操作方法通過參數(shù)來接收所提交的表單數(shù)據(jù)。
??操作方法將參數(shù)聲明為IFormCollection類型,表單提交的會存放在IFormCollection實例中,其數(shù)據(jù)結(jié)構(gòu)類型字典。
- 例
??定義Demo控制器。PostUp方法在客戶端提交后從form參數(shù)提取數(shù)據(jù)存到字典中,再把字典傳遞給Show視圖。
public class DemoController : Controller
{
public ActionResult Default() => View();
public ActionResult PostUp(IFormCollection form)
{
IDictionary<string, string> dic = new Dictionary<string, string>();
foreach(string k in form.Keys)
{
string v = form[k];
dic[k] = v;
}
return View("Show", dic);
}
}
??Default視圖。
<html>
<body>
<table>
<tr>
<td><label for="city">城市:</label></td>
<td><input type="text" name="city" form="form"/></td>
</tr>
<tr>
<td> <label for="name">姓名:</label></td>
<td><input name="name" type="text" form="form"/></td>
</tr>
</table>
<form id="form" asp-controller="Demo" asp-action="PostUp">
<input type="submit" value="提交"/>
</form>
</body>
</html>
??Show視圖顯示內(nèi)容。@model指令用于聲明該視圖接收的模型對象為字典類型,隨后Model屬性可獲取字典的實例引用,并用foreach語句枚舉出子項中Key與Value值。
@model IDictionary<string, string>
<html>
<body>
<h3>你輸入的內(nèi)容</h3>
@{
foreach(var i in Model)
{
<p>@i.Key : @i.Value</p>
}
}
</body>
</html>