简体   繁体   English

在控制器中使用Area时Url.Action()的结果

[英]Result of Url.Action() when Area is used in Controller

I'm reading Microsoft documentation where it explains about the usage of Area for routing https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.2#areas . 我正在阅读Microsoft文档,其中解释了有关Area用于路由https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.2#areas的用法。

There is 1 part I can't understand. 有1个部分我无法理解。

app.UseMvc(routes =>
{
    routes.MapAreaRoute("duck_route", "Duck",
        "Manage/{controller}/{action}/{id?}");
    routes.MapRoute("default", "Manage/{controller=Home}/{action=Index}/{id?}");
});

Controller: 控制器:

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

Why is inside GenerateURLInArea() action, it returns /Manage whereas in GenerateURLOutsideOfArea() it returns /Manage/Home/Index ? 为什么在GenerateURLInArea()动作中返回/Manage而在GenerateURLOutsideOfArea()返回/Manage/Home/Index呢?

@itminus highlighted that the results showed the opposite when it was executed. @itminus突出显示,执行结果相反。 I've also confirmed that it is true. 我也已经证实这是真的。

So this should be the expected behavior 所以这应该是预期的行为

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

This actually makes more sense :) 这实际上更有意义:)

@itminus also has raised a ticket for this fix https://github.com/aspnet/AspNetCore.Docs/issues/12221/ @itminus还为该修复程序发出了罚单https://github.com/aspnet/AspNetCore.Docs/issues/12221/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM