如何在控制器动作中使用路由参数?

时间:2011-04-15 17:23:38

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

我非常擅长在ASP.NET MVC中进行路由。

我有以下控制器:

    public ActionResult Index(int? categoryId)
    {
        IList<Item> result = categoryId == null ? _itemsService.GetLast(20) : _itemsService.GetByCategory((int)categoryId);
        var viewModel = Mapper.Map<IList<Item>, IList<ItemsIndexViewModel>>(result);
        return View(viewModel);
    }

可以为null的categoryId参数引用子类别的id。如果没有传递任何内容,则必须显示最后20个项目,但如果传递了子类别ID,则应显示该类别中的项目。

但我想要的是:www.myDomain.com/Category/SubCategory(例如:/Electronics/Cell-Phones

我尝试写一条路线是这样的:

        routes.MapRoute(
            "ItemIndex",
            "{category}/{subcategory}",
            new {controller = "Item", action = "Index", categoryId = UrlParameter.Optional}
            );

但我不知道如何传递类别和子类别的值。

任何帮助将不胜感激:)

更新 以下是我到目前为止的路线定义:

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

        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" });

        routes.MapRoute(
                "ItemDetail",
                "Item/Detail/{itemId}",
                new { controller = "Item", action = "Detail" }
            );

        routes.MapRoute(
            "ItemIndex",
            "Items/{category}/{subcategory}",
            new { controller = "Item", action = "Index", subcategory = UrlParameter.Optional }
            );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

这是我的控制器我希望将路线映射到:

    public ActionResult Index(string category, string subcategory)
    {
       // IList<Item> result = string.IsNullOrEmpty(subcategory) ? _itemsService.GetLast(20) : _itemsService.GetByCategory(subcategory);
        IList<Item> result;
        if (string.IsNullOrEmpty(subcategory))
        {
            if (string.IsNullOrEmpty(category))
            {
                result = _itemsService.GetLast(20);
            }
            else
            {
                result = _categoryService.GetItemsInTopLevel(category);
            }
        } else
        {
            result = _itemsService.GetByCategory(subcategory);
        }
        var viewModel = Mapper.Map<IList<Item>, IList<ItemsIndexViewModel>>(result);
        return View(viewModel);
    }

以下是我从视图中调用它的方式:

@model IList<Sharwe.MVC.Models.ParentCategory>

<div id="sharwe-categories">
    <ul class="menu menu-vertical menu-accordion">
        @foreach(var topLevel in Model)
        {
             <li class="topLevel">
                <h3>  
                    @Html.ActionLink(topLevel.Name, "Index", "Item", new { category = topLevel.Name }, new {@class = "main"} )
                    <a href="#" class="drop-down"></a>
                </h3>
                <ul>
                    @foreach (var childCategory in topLevel.Children)
                    {
                        <li>@Html.ActionLink(childCategory.Name, "Index", "Item", new RouteValueDictionary{ { "category" , topLevel.Name }, { "subcategory" , childCategory.Name }})</li>
                    }
                </ul>
            </li>
        }

    </ul>

</div>

当我点击顶级类别时,它的效果非常好。但是点击子类别不起作用。它实际上重定向到http://localhost/?Length=4。我不知道这是从哪里来的......

1 个答案:

答案 0 :(得分:2)

将它们作为参数

传递给控制器​​操作方法
public ActionResult Index(string category, string subcategory)
{
    if (string.IsNullOrEmpty(subcategory))
    {
        // display top 20
    }
    else
    {
        // display subcategory
    }
}

字符串已经是引用类型,因此您不必设置任何其他内容。