【问题标题】:MVC 4 - Hide Controller In URLMVC 4 - 在 URL 中隐藏控制器
【发布时间】:2023-03-06 15:28:02
【问题描述】:

我有一个区域寄存器路由:

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "MyArea_default",
            "MyArea/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new string[] { "market.Areas.MyArea.Controllers" }
        );
    }

它是这样工作的,没关系:

localhost:MyArea/ActionName

但我在选择特定视图时按 F5(在 Visual Studio 中),然后 url 是:

localhost:MyArea/Home/ActionName

如果选择了特定视图,当我按 f5 时,如果没有控制器名称,如何在 url 中工作?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-routing


    【解决方案1】:

    我从你的代码中了解到的:

    localhost:MyArea/ActionName => 将使用 homeControllerClass 和 ActionName 方法。

    localhost:MyArea/Home/ActionName => 将不起作用,因为它使用 homeControllerClass 但 ActionName "Home" !就是在 homeControllerClass 中寻找 home 方法。

    来自您的代码: “我的区域/{action}/{id}” 新的 { 控制器 = “主页”,动作 = “索引”,id = UrlParameter.Optional }

    => 如果未指定任何操作,则意味着对 MyArea 的请求使用 homecontroller 类,默认操作名称为 index。 因此,如果您使用 MyArea/Home =>,它将使用 homeController,但使用“Home”操作。

    {控制器}/{动作}

    【讨论】:

      【解决方案2】:

      最简单的解决方案是使用服装路线属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多