【问题标题】:Writing a custom aspnet mvc action without an action编写没有操作的自定义 aspnet mvc 操作
【发布时间】:2010-06-01 22:22:04
【问题描述】:

我正在寻找一个允许以下操作的自定义路由

http://localhost/blog/tags/foo

目前这是实际可行的

http://localhost/tags/Index/nhibernate

我尝试了以下但没有成功 - 任何帮助将不胜感激

routes.MapRoute(
    "Tags",
    "{controller}/{id}",
    new { Controller = "Tags", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    "Tags",
    "blog/{controller}/{id}",
    new { Controller = "Tags", action = "Index", id = "" }
);

【问题讨论】:

    标签: asp.net-mvc routing


    【解决方案1】:

    您可以在 global.asax 中使用如下内容:

    routes.MapRoute("Tags",
                    "blog/tags/{TagName}", 
                    new { Controller = "Tags", action = "ShowTag", TagName = "" });
    

    然后,您需要一个名为“TagsController.cs”的控制器,该控制器具有一个名为 ShowTag 的 ActionResult 方法以及一个名为 ShowTag.aspx 的相应 aspx。您的 ShowTag 方法应如下所示:

    public ActionResult ShowTag(string TagName)
    {
        //do stuff here to get Id from tag name and get other data etc...
        return View();
    }
    

    请注意,您在 Global.asax.cs 中映射路线的顺序确实很重要。

    【讨论】:

    • 老兄,你摇滚!原来订单是问题所在。我把这条标签路线放在第一位,一切都很好!非常感谢!
    • 没问题,路线顺序让我难倒了好几次!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多