最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解ASP.NET Core 2.0 路由引擎之網(wǎng)址生成(譯)

 更新時(shí)間:2017年11月04日 11:55:04   作者:三生石上(FineUI控件)  
這篇文章主要介紹了詳解ASP.NET Core 2.0 路由引擎之網(wǎng)址生成(譯),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

問(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

襄垣县| 丰原市| 忻州市| 如东县| 卢湾区| 鹤岗市| 阳城县| 满城县| 彭阳县| 修文县| 通许县| 宁德市| 扎鲁特旗| 泰顺县| 大丰市| 长阳| 望谟县| 蒙城县| 彭阳县| 霍林郭勒市| 临泽县| 横峰县| 延川县| 郓城县| 伊宁县| 尤溪县| 涡阳县| 铁力市| 盐亭县| 科技| 余江县| 隆德县| 略阳县| 达拉特旗| 涿州市| 从化市| 吉隆县| 闻喜县| 迭部县| 石家庄市| 无为县|