【问题标题】:Removing Action name from URL从 URL 中删除操作名称
【发布时间】:2013-07-26 18:11:08
【问题描述】:

我有一个这样的网站 -

  • 主页 - www.mysite.com。

  • 类别页面 - www.mysite.com/Categories。 (从主页链接)

在“类别”页面上,我有一个类别链接列表。

如果有人选择了一个类别,我想显示这样的 URL -

  • (选择 CategoryA 后)- www.mysite.com/Categories/CategoryA

  • (选择 CategoryB 后)- www.mysite.com/Categories/CategoryB

目前,我的网址显示如下 - www.mysite.com/Categories/Index/CategoryA

我有一个HomeController 和一个CategoriesController。 CategoriesController 有一个名为 Index 的 Action,其中不包括一个 Category,它是单词 Index 的来源。

如何去掉Index这个词?

我尝试映射Route,如下所示,但它不起作用 -

        routes.MapRoute(
            name: "CategoriesPage",
            url: "Categories/{id}",
            defaults: new { controller = "Categories", action = "Index", id = UrlParameter.Optional }
        );

【问题讨论】:

标签: asp.net-mvc


【解决方案1】:

我的解决方案如下 -

我像这样映射了Route -

        routes.MapRoute(
            "CategoryPage",
            "Categories/{id}",
            new { controller = "Categories", action = "Index" }
        );

在我的View 中,我的ActionLink 看起来像这样 -

@Html.ActionLink("CategoryDescription", "Index", new { controller = "Categories", id = c.Id })

(这个答案让我找到了这个解决方案 - Routing: How to hide action name in url?)

【讨论】:

    【解决方案2】:

    /* 这只是动作名称 /
    routes.MapRoute("custom_name1", "sitemap", new { controller = "home", action = "sitemap", id = UrlParameter.Optional }); /
    这仅适用于控制器名称 */
    routes.MapRoute("custom_name2", "home", new { controller = "home", action = "sitemap", id = UrlParameter.Optional });

    【讨论】:

    • 请提供更多详细信息
    猜你喜欢
    • 1970-01-01
    • 2013-07-22
    • 2020-09-09
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多