【问题标题】:Incorrect URL after changing routing in Area在区域中更改路由后 URL 不正确
【发布时间】:2018-08-06 06:29:40
【问题描述】:

我在更改路由后无法呈现 URL 地址。

路由配置

public class AccountArea : AreaRegistration
{
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Account",
            "Account/{controller}/{action}/{id}",
            new { controller = "User", action = "Index", id = UrlParameter.Optional },
            namespaces:  new[] {
                "Application.Controllers.Account"
            }
        );
    }

    public override string AreaName => "AccountArea";
}

public class FrontArea : AreaRegistration
{
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Front",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces:  new[] {
                "Application.Controllers.Front"
            }
        );
    }

    public override string AreaName => "FrontArea";
}

RouteConfig:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    }

控制器

// on folder Controllers/Front/
namespace Application.Controllers.Front
class HomeController : Controller
{
   ActionResult Index(){ ... }
}

// on folder Controllers/Account/
Application.Controllers.Account
class UserController : Controller
{
   ActionResult Index(){...}
}

我更改了路由,因为我想将控制器分成 2 个子文件夹,即 Front 和 Account。

此配置允许我将应用程序分为两部分,使用 URL 地址进行区分。

localhost/Home/Index [namespace: Application.Controllers.Front]
localhost/Account/User/Index- [namespace: Application.Controllers.Account] 

在浏览器中输入地址后

localhost/Home/Index - will start method Index from HomeController
localhost/Account/User/Index - will start method Index from UserController

在 URL 地址 localhost/Home/Index 上出现问题,我尝试呈现属于 localhost/Account/User/Index 的 URL 地址。

当我使用@Url.Action("Index","User") 时,地址将被呈现,但 URL 中没有“Account”前缀。

我收到:/User/Index

但是,我希望:/Account/User/Index

反之亦然,在 localhost/Account/User/Index 上,我无法呈现属于 localhost/Home/Index 的正确 URL 地址。

这个问题我可以用Url.RouteUrllike解决

@Url.RouteUrl("Account", new {controller = "User", account = "Index"} )

但是,这需要路由名称,我不想提供。

【问题讨论】:

    标签: asp.net-mvc routes url-routing asp.net-mvc-routing asp.net-mvc-areas


    【解决方案1】:

    使用区域路由时,所有UrlHelper URL 解析方法(如ActionLinkUrl.Action)默认使用相同区域

    从前端区域链接到前端区域

    @Url.Action("Index", "User")
    

    要指定去一个不同的区域,您必须明确在传递给Url.Action的路由值中指定该区域。

    从前台区域链接到帐户区域

    @Url.Action("Index", "User", new { area = "Account" })
    

    Account 区域内生成的链接也是如此。您需要使用Front 显式覆盖当前区域名称。

    从帐户区域链接到前台区域

    @Url.Action("Index", "User", new { area = "Front" })
    

    这需要路线名称,我不想提供。

    选项 1 - 对区域使用常量

    为应用程序中的所有区域字符串创建一组常量。

    public static class Areas
    {
        public const string Front = "Front";
        public const string Account = "Account";
    }
    

    并使用喜欢

    @Url.Action("Index", "User", new { area = Areas.Front })
    

    选项 2 - 使用 T4MVC

    有一个名为 T4MVC 的包可为您的项目生成常量,因此您可以在不使用魔术字符串的情况下替换路由和 URL 生成。

    @Url.Action(MVC.Areas.Front.User.Index)
    

    选项 3 - 使用扩展方法

    另一种选择是构建自己的扩展方法来替换 Url.ActionHtml.ActionLinkRedirectToAction 等。

    @Url.ActionToFront("Index", "User")
    

    不过,要创建很多操作方法。

    【讨论】:

    • 感谢回复,但我写道我不想填写区域或路线名称:),这适用于命名空间,此示例可以检测命名空间,如果我使用 URL 地址 localhost/User/Index我收到 404,但如果我使用 localhost/Account/User/Index 这个匹配命名空间女巫路线并正常工作:)
    • 我正在考虑 ovveride 或扩展 UrlHelper,方法是使用 typeof().Namespace 查找 Controler 命名空间,将此命名空间与路由匹配并返回 Url.RouteUrl("recived_route", .... ) 这个扩展应该在每个 Html 方法上实现,比如 Partial、RenderAction、....
    • 使用扩展方法可能会奏效,但链接到同一区域的其他地方通常比链接到其他区域更常见,所以这不是t 和你想象的一样大。您可以制作扩展方法,但基本上只是将问题转移到另一个地方。 RouteUrl 也解决不了多少问题——如果你不将区域名称指定为路由值,它的工作方式与Url.Action 相同——唯一的区别是你可以选择指定路由名称以将匹配范围缩小到单条路线。
    • 我不喜欢在代码中硬编码字符串:)这个选项可能没问题,如果编译器会在构建项目时检查这个:)
    • 然后我建议创建一个名为Areas 的静态类并为您的区域名称创建常量。然后,您可以构建像@Url.Action("Index", "User", new { area = Areas.Front }) 这样的 URL。或者,使用T4MVC,它允许您在不使用魔术字符串的情况下配置 MVC。
    猜你喜欢
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 2019-03-28
    • 2019-09-13
    • 1970-01-01
    • 2018-06-24
    相关资源
    最近更新 更多