Result of Url.Action() when Area is used in Controller
我正在阅读 Microsoft 文档,其中解释了使用
有一段我听不懂。
1 2 3 4 5 6 | app.UseMvc(routes => { routes.MapAreaRoute("duck_route","Duck", "Manage/{controller}/{action}/{id?}"); routes.MapRoute("default","Manage/{controller=Home}/{action=Index}/{id?}"); }); |
控制器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using Microsoft.AspNetCore.Mvc; namespace MyApp.Namespace4 { [Area("Duck")] public class UsersController : Controller { public IActionResult GenerateURLInArea() { // Uses the 'ambient' value of area var url = Url.Action("Index","Home"); // returns /Manage return Content(url); } public IActionResult GenerateURLOutsideOfArea() { // Uses the empty value for area var url = Url.Action("Index","Home", new { area ="" }); // returns /Manage/Home/Index return Content(url); } } } |
为什么在
@itminus 强调执行时结果显示相反。我也确认这是真的。
所以这应该是预期的行为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | using Microsoft.AspNetCore.Mvc; namespace MyApp.Namespace4 { [Area("Duck")] public class UsersController : Controller { public IActionResult GenerateURLInArea() { // Uses the 'ambient' value of area var url = Url.Action("Index","Home"); // returns /Manage/Home/Index return Content(url); } public IActionResult GenerateURLOutsideOfArea() { // Uses the empty value for area var url = Url.Action("Index","Home", new { area ="" }); // returns /Manage return Content(url); } } } |
这实际上更有意义:)
@itminus 也为此修复提出了一张票 https://github.com/aspnet/AspNetCore.Docs/issues/12221/