【问题标题】:html.actionlink not creating right linkshtml.actionlink 没有创建正确的链接
【发布时间】:2012-01-07 07:30:51
【问题描述】:

我在 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

但它不会创建这些 url。

我是不是做错了什么。

我们将不胜感激。

//在这里更新我的问题

其实这个是可行的

@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 作为最后一个参数传递。

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

干杯

帕米德

【问题讨论】:

  • 你实际生成的链接是什么?
  • @Floradu88 它正在生成主页链接 (localhost)

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


【解决方案1】:

试试

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

【讨论】:

  • 谢谢,3nigma。有用。我已经更新了我的问题。所以另一种选择也有效。
  • 3nigma 你不能至少在一段时间内专注于你的考试:P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 2016-04-03
  • 2013-11-06
  • 2016-12-29
  • 1970-01-01
  • 2012-03-21
相关资源
最近更新 更多