C# 创建指向动态内容页的Html.action链接

C# 创建指向动态内容页的Html.action链接,c#,asp.net,asp.net-mvc,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 3,我的网站上有为前端创建/编辑/删除页面的功能。这是我的控制器: namespace MySite.Controllers { public class ContentPagesController : Controller { readonly IContentPagesRepository _contentPagesRepository; public ContentPagesController() {

我的网站上有为前端创建/编辑/删除页面的功能。这是我的控制器:

namespace MySite.Controllers
{
    public class ContentPagesController : Controller
    {
        readonly IContentPagesRepository _contentPagesRepository;

        public ContentPagesController()
        {
            MyDBEntities entities = new MyDBEntities();
            _contentPagesRepository = new SqlContentPagesRepository(entities);
        }


        public ActionResult Index(string name)
        {
            var contentPage = _contentPagesRepository.GetContentPage(name);

            if (contentPage != null)
            {
                return View(new ContentPageViewModel
                {
                    ContentPageId = contentPage.ContentPageID,
                    Name = contentPage.Name,
                    Title = contentPage.Title,
                    Content = contentPage.Content
                });
            }

            throw new HttpException(404, "");
        }
    }
}
在my global.asax中:

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

    routes.MapRoute(
        "Page", // Route name
        "Page/{name}", // URL with parameters
        new { controller = "ContentPages", action = "Index" }, // Parameter defaults
        new[] { "MySite.Controllers" }
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new[] { "MySite.Controllers" }
    );
}
所以我的数据库中有一个动态页面,名为About。如果我访问mysite.com/Page/About,我可以查看动态内容

我想创建指向此页面的ActionLink。我试过这样做:

@Html.ActionLink("About Us", "Index", "ContentPages", new { name = "About" })
@Html.ActionLink(
    "About Us",                // linkText
    "Index",                   // action
    "ContentPages",            // controller
    new { name = "About" },    // routeValues
    null                       // htmlAttributes
)
但是当我查看页面上的链接时,url只会转到查询字符串中
Length=12
的当前页面。例如,如果我在主页上,链接会转到
mysite.com/Home?Length=12


我做错了什么?

您没有使用正确的ActionLink重载。试着这样做:

@Html.ActionLink("About Us", "Index", "ContentPages", new { name = "About" })
@Html.ActionLink(
    "About Us",                // linkText
    "Index",                   // action
    "ContentPages",            // controller
    new { name = "About" },    // routeValues
    null                       // htmlAttributes
)
鉴于在你的例子中:

@Html.ActionLink(
    "About Us",                // linkText
    "Index",                   // action
    "ContentPages",            // routeValues
    new { name = "About" }    // htmlAttributes
)
这很明显地解释了为什么你没有生成预期的链接