詳解ASP.NET Core 2.0 路由引擎之網(wǎng)址生成(譯)
問(wèn)題
如何在ASP.NET Core 2.0中由路由引擎來(lái)生成網(wǎng)址?
答案
新建一個(gè)空項(xià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?}");
});
}
添加一個(gè)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的路由機(jī)制來(lái)生成網(wǎng)址,而無(wú)需在應(yīng)用程序中硬編碼網(wǎng)址。MVC有這么做的所有信息,來(lái)自于我們?cè)O(shè)置路由映射所提供的模板。
MVC提供了IUrlHelper接口來(lái)提供生成網(wǎng)址的功能。這是通過(guò)在控制器基類,視圖和試圖組件公開(kāi)Url屬性來(lái)實(shí)現(xiàn)的。
IUrlHelper接口提供兩個(gè)關(guān)鍵的方法來(lái)生成網(wǎng)址:
1.Action:通過(guò)提供控制器,方法和路由參數(shù)值來(lái)生成網(wǎng)址。
2.RouteUrl: 通過(guò)提供路由映射名稱和路由參數(shù)來(lái)生成網(wǎng)址。
如果調(diào)用上述方法時(shí)未提供控制器和路由參數(shù),那么MVC會(huì)從當(dāng)前請(qǐng)求或者方法參數(shù)中獲?。词菑漠?dāng)前上下文的環(huán)境變量中獲?。?。下面的方法存在于MobileController控制器中:
public IActionResult PageTwo(int id)
{
var url = Url.Action("PageTwo"); // /mobile/PageTwo/1
return Content($"(POST) Mobile/Two: {id} (Url: {url})");
}
路由參數(shù)可以作為匿名對(duì)象來(lái)提供:
public IActionResult PageThree()
{
var url = Url.RouteUrl("goto_two", new { id = 5 }); // /two/5
return Content($"Mobile/Three (Url: {url})");
}

如果MVC無(wú)法將這些值映射到地址標(biāo)記,那么這些參數(shù)會(huì)作為網(wǎng)址的查詢字符串拼接起來(lái):
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類上有一個(gè)很方便的方法RedirectToAction,用來(lái)將用戶請(qǐng)求重定向到某個(gè)控制器方法中,這一過(guò)程是在客戶端完成的:
public IActionResult PageFive()
{
return RedirectToAction("PageSix");
}
public IActionResult PageSix()
{
return Content("Mobile/Six (Mobile/Five will also come here)");
}
為了將IUrlHeper作為依賴項(xiàng)注入需要的類中,我們需要首先在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();
}
注:大部分情況下我們無(wú)需通過(guò)注入來(lái)使用IUrlHelper,因?yàn)榭刂破?,視圖中都已經(jīng)公開(kāi)了Url屬性供我們使用。
原文:https://tahirnaushad.com/2017/08/20/asp-net-core-mvc-routing/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?Core使用EF創(chuàng)建模型(包含屬性、排除屬性、主鍵和生成值)
- ASP.NET Core自動(dòng)生成小寫(xiě)破折號(hào)路由的實(shí)現(xiàn)方法
- ASP.NET Core 5中如何生成PDF文檔
- Asp.Net Core使用swagger生成api文檔的完整步驟
- Asp.NetCore1.1版本去掉project.json后如何打包生成跨平臺(tái)包
- Asp.net core WebApi 使用Swagger生成幫助頁(yè)實(shí)例
- 基于ASP.NET Core數(shù)據(jù)保護(hù)生成驗(yàn)證token示例
- asp.net core實(shí)現(xiàn)在線生成多個(gè)文件將多個(gè)文件打包為zip返回的操作
相關(guān)文章
.Net 調(diào)用存儲(chǔ)過(guò)程取到return的返回值
存儲(chǔ)過(guò)程只能返回 int 類型,如果返回一個(gè)字符串 ,將會(huì)報(bào)類型轉(zhuǎn)化錯(cuò)誤,下面以示例介紹下如何取到return的值,需要的朋友可以參考下2014-08-08
asp.net updatepanel 導(dǎo)致JS不能加載,而無(wú)法使用的解決方法
asp.net updatepanel 局部刷新,導(dǎo)致JS不能加載,而無(wú)法使用,而且 updatepanel會(huì)刷兩次,郁悶的,解決方法如下2013-08-08
詳解ASP.NET Core WebApi 返回統(tǒng)一格式參數(shù)
這篇文章主要介紹了詳解ASP.NET Core WebApi 返回統(tǒng)一格式參數(shù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
asp.net微信開(kāi)發(fā)(開(kāi)發(fā)者接入)
這篇文章主要介紹了asp.net微信開(kāi)發(fā)中有關(guān)開(kāi)發(fā)者接入的相關(guān)內(nèi)容,需要的朋友可以參考下2015-11-11
asp.net中生成縮略圖并添加版權(quán)實(shí)例代碼
這篇文章介紹了asp.net中生成縮略圖并添加版權(quán)實(shí)例代碼,有需要的朋友可以參考一下2013-11-11
使用.net?core?自帶DI框架實(shí)現(xiàn)延遲加載功能
在某些情況,我們希望能延遲一個(gè)依賴的初始化。如果使用的是autofac,我們可以通過(guò)注入Lazy來(lái)實(shí)現(xiàn),這篇文章主要介紹了使用.net?core?自帶DI框架實(shí)現(xiàn)延遲加載,需要的朋友可以參考下2023-02-02

