html.actionlink没有创建正确的链接

时间:2012-01-07 07:30:51

标签: c# asp.net-mvc asp.net-mvc-3 routes

我在global.asax

中配置了这样的路由
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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


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

这是我的两个控制器

public class ProfileController : BaseController
{
    public  ActionResult Index(int id, string name)
    {
        return View();
    }
}

public class PoemsController : BaseController
{

    public ActionResult Index(int id, string name)
    {
        return View();
    }
}

在主页的某个地方,我有像这样的HTML动作链接

@Html.ActionLink("Profile", "index", "Profile", new { id = 1, name = "someuser" })

@Html.ActionLink("Poems by ", "index", "poems", new { id = 1, name = "someuser" })

我期待两个网址,比如

http://localhost/profile/1/someuser
http://localhost/profile/1/someuser/poems

但它并没有创建这些网址。

我做错了什么。

帮助将不胜感激。

//在这里更新我的问题

实际上这个有用

@Html.ActionLink("Profile", "index", "Profile", new { id = 1, name = "someuser" },null)

@Html.ActionLink("Poems by ", "index", "poems", new { id = 1, name = "some user" },null)

将null作为最后一个参数传递。

不知道为什么,但它确实有效。

干杯

成员Parminder

1 个答案:

答案 0 :(得分:1)

@Html.ActionLink("Profile", "index", new {controller="Profile" id = 1, name = "someuser" })
相关问题