[譯]ASP.NET Core 2.0 路由引擎之網址生成

三生石上(FineUI控制元件)發表於2017-11-02

問題

如何在ASP.NET Core 2.0中由路由引擎來生成網址?

答案

新建一個空專案,修改Startup.cs檔案,新增MVC服務和中介軟體:

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的路由機制來生成網址,而無需在應用程式中硬編碼網址。MVC有這麼做的所有資訊,來自於我們設定路由對映所提供的模板。

MVC提供了IUrlHelper介面來提供生成網址的功能。這是通過在控制器基類,檢視和試圖元件公開Url屬性來實現的。

IUrlHelper介面提供兩個關鍵的方法來生成網址:

  1. Action:通過提供控制器,方法和路由引數值來生成網址。
  2. RouteUrl: 通過提供路由對映名稱和路由引數來生成網址。

如果呼叫上述方法時未提供控制器和路由引數,那麼MVC會從當前請求或者方法引數中獲取(即是從當前上下文的環境變數中獲取)。下面的方法存在於MobileController控制器中:

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})");
}

如果MVC無法將這些值對映到地址標記,那麼這些引數會作為網址的查詢字串拼接起來:

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中配置相應的服務:

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,因為控制器,檢視中都已經公開了Url屬性供我們使用。

 

原始碼下載

 

原文:https://tahirnaushad.com/2017/08/20/asp-net-core-mvc-routing/

相關文章