帮助我使ASP.NET MVC2 RC ActionLink工作?

时间:2010-02-26 03:52:28

标签: asp.net-mvc

我可以拥有SWORN我有这个工作,但在某条线上,我显然打破了它。也许是在我从VS2008中的ASP.NET MVC迁移到VS2010中的ASP.NET MVC2时。

我的动作链接:

Html.ActionLink(segment.TitleWithChapterNumber, "Index", "Read", new { bookCode = Model.Product.Id, segmentCode = segment.Index }, null)

我希望它匹配的路线:

routes.MapRoute(
    "Read",
    "Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
    new { controller = "Read", action = "Index", bookCode = "", segmentCode = "", sectionCode = "", renderStrategy = "" }
);

这会呈现如下链接:http://localhost/Obr/Read?bookCode=14&segmentCode=0 但我希望它看起来像http://localhost/Obr/Read/14/0 单击它呈现的链接会将我带到正确的控制器,并且响应是准确的。如果我粘贴链接我希望它看起来像,它确实有效。我猜它只是不匹配?

我错过了一些明显的东西吗?我已经盯着它看了很久,我甚至不知道我在找什么。

供参考,以下是我的所有路线:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    "ReadImage",
    "Read/Image/{bookId}/{imageName}",
    new { controller = "Read", action = "Image" }
);

routes.MapRoute(
    "Read",
    "Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
    new { controller = "Read", action = "Index", bookCode = "", segmentCode = "", sectionCode = "", renderStrategy = "" }
);

routes.MapRoute(
    "BookReport",
    "BookReport/{action}/{folder}",
    new { controller = "BookReport", action = "Details", folder = "" }
);

routes.MapRoute(
    "Reference",
    "Reference/Details/{referenceType}/{searchText}",
    new { controller = "Reference", action = "Details", referenceType = "", searchText = "" }
);

routes.MapRoute(
    "PaginatedAudits",                                      // Route name
    "Audit/Page/{pageNumber}",                              // URL with parameters
    new { controller = "Audit", action = "Index" }          // Parameter defaults
);

routes.MapRoute(
    "PaginatedReadLog",                                     // Route name
    "ReadLog/Page/{pageNumber}",                            // URL with parameters
    new { controller = "ReadLog", action = "Index" }        // Parameter defaults
);

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

动作签名如下所示:

[Authorize]
public ActionResult Index(string bookCode, string segmentCode, string sectionCode, string renderStrategy)
{
    // code
}

2 个答案:

答案 0 :(得分:0)

通过明确指定路线名称来尝试路线链接:

Html.RouteLink(
    segment.TitleWithChapterNumber, // linkText
    "Read", // routeName
    new { 
        bookCode = Model.Product.Id, 
        segmentCode = segment.Index 
    }, // routeValues
    null // htmlAttributes
)

答案 1 :(得分:0)

您的路线定义应该包含UrlParameter.Optional而不是空字符串。

routes.MapRoute(
    "Read",
    "Read/{bookCode}/{segmentCode}/{sectionCode}/{renderStrategy}",
    new { controller = "Read", action = "Index", bookCode = UrlParameter.Optional, segmentCode = UrlParameter.Optional, sectionCode = UrlParameter.Optional, renderStrategy = UrlParameter.Optional }
);

这也有助于重定向控制器和通过MVC扩展创建的表单网址。