詳解ASP.NET Core 2.0 路由引擎之網(wǎng)址生成(譯)
問題
如何在ASP.NET Core 2.0中由路由引擎來生成網(wǎng)址?
答案
新建一個空項目,修改Startup.cs文件,添加MVC服務(wù)和中間件:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(routes => { routes.MapRoute( name: "goto_one", template: "one", defaults: new { controller = "Home", action = "PageOne" }); routes.MapRoute( name: "goto_two", template: "two/{id?}", defaults: new { controller = "Home", action = "PageTwo" }); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
添加一個MobileController控制器類:
public class MobileController : Controller { public IActionResult Index() { var url = Url.Action("Index"); // /mobile return Content($"Mobile/Index (Url: {url})"); } public IActionResult PageOne() { var url = Url.Action("PageOne"); // /mobile/PageOne return Content($"Mobile/One (Url: {url})"); } [HttpGet] public IActionResult PageTwo() { var url = Url.Action("PageTwo"); // /mobile/PageTwo OR /mobile/PageTwo/1? return Content($"(GET) Mobile/Two (Url: {url})"); } [HttpPost] public IActionResult PageTwo(int id) { var url = Url.Action("PageTwo"); // /mobile/PageTwo/1 return Content($"(POST) Mobile/Two: {id} (Url: {url})"); } public IActionResult PageThree() { var url = Url.RouteUrl("goto_two", new { id = 5 }); // /two/5 return Content($"Mobile/Three (Url: {url})"); } public IActionResult PageFour() { var url = Url.RouteUrl("goto_two", new { q = 5 }); // /two?q=5 return Content($"Mobile/Four (Url: {url})"); } public IActionResult PageFive() { return RedirectToAction("PageSix"); } public IActionResult PageSix() { return Content("Mobile/Six (Mobile/Five will also come here)"); } }
討論
我們可以使用MVC的路由機制來生成網(wǎng)址,而無需在應(yīng)用程序中硬編碼網(wǎng)址。MVC有這么做的所有信息,來自于我們設(shè)置路由映射所提供的模板。
MVC提供了IUrlHelper接口來提供生成網(wǎng)址的功能。這是通過在控制器基類,視圖和試圖組件公開Url屬性來實現(xiàn)的。
IUrlHelper接口提供兩個關(guān)鍵的方法來生成網(wǎng)址:
1.Action:通過提供控制器,方法和路由參數(shù)值來生成網(wǎng)址。
2.RouteUrl: 通過提供路由映射名稱和路由參數(shù)來生成網(wǎng)址。
如果調(diào)用上述方法時未提供控制器和路由參數(shù),那么MVC會從當(dāng)前請求或者方法參數(shù)中獲?。词菑漠?dāng)前上下文的環(huán)境變量中獲?。O旅娴姆椒ù嬖谟贛obileController控制器中:
public IActionResult PageTwo(int id) { var url = Url.Action("PageTwo"); // /mobile/PageTwo/1 return Content($"(POST) Mobile/Two: {id} (Url: {url})"); }
路由參數(shù)可以作為匿名對象來提供:
public IActionResult PageThree() { var url = Url.RouteUrl("goto_two", new { id = 5 }); // /two/5 return Content($"Mobile/Three (Url: {url})"); }
如果MVC無法將這些值映射到地址標記,那么這些參數(shù)會作為網(wǎng)址的查詢字符串拼接起來:
public IActionResult PageFour() { var url = Url.RouteUrl("goto_two", new { id=5, key1 = "value1" }); // /two/5?key1=value1 return Content($"Mobile/Four (Url: {url})"); }
ControlBase類上有一個很方便的方法RedirectToAction,用來將用戶請求重定向到某個控制器方法中,這一過程是在客戶端完成的:
public IActionResult PageFive() { return RedirectToAction("PageSix"); } public IActionResult PageSix() { return Content("Mobile/Six (Mobile/Five will also come here)"); }
為了將IUrlHeper作為依賴項注入需要的類中,我們需要首先在ConfigureServices中配置相應(yīng)的服務(wù):
public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IActionContextAccessor, ActionContextAccessor>(); services.AddScoped<IUrlHelper>(factory => { var actionContext = factory.GetService<IActionContextAccessor>().ActionContext; return new UrlHelper(actionContext); }); services.AddMvc(); }
注:大部分情況下我們無需通過注入來使用IUrlHelper,因為控制器,視圖中都已經(jīng)公開了Url屬性供我們使用。
原文:https://tahirnaushad.com/2017/08/20/asp-net-core-mvc-routing/
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net中將js的返回值賦給asp.net控件的小例子
要做一個顯示用戶在線停留時間的功能,拖了一個label控件用于顯示時間,而時間是通過js來實現(xiàn)的,現(xiàn)在要把js的返回值賦給label,方法如下:2013-03-03詳解Asp.Net Core 2.1+的視圖緩存(響應(yīng)緩存)
本篇文章給大家通過實例講述了Asp.Net Core 2.1+的視圖緩存(響應(yīng)緩存)的相關(guān)知識點,對此有興趣的讀者們可以學(xué)習(xí)下。2018-03-03創(chuàng)建一個ASP.NET MVC5項目的實現(xiàn)方法(圖文)
這篇文章主要介紹了創(chuàng)建一個ASP.NET MVC 5項目,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09ASP.NET?Core使用EF?SQLite對數(shù)據(jù)庫增刪改查
這篇文章介紹了ASP.NET?Core使用EF?SQLite對數(shù)據(jù)庫增刪改查的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01解決Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的方法
這篇文章主要介紹了解決Asp.net Mvc返回JsonResult中DateTime類型數(shù)據(jù)格式問題的方法,需要的朋友可以參考下2016-06-06