【问题标题】:ASP.net MVC Areas and creating an ActionLink with ID (SEO / clean URL)ASP.net MVC 区域并创建带有 ID 的 ActionLink(SEO / 干净 URL)
【发布时间】:2010-11-19 18:32:46
【问题描述】:

我正在使用 ASP.NET MVC 1.0 / C# 为客户构建帮助台票证系统。我已经实现了 Steven Sanderson 的“App Areas in ASP.NET MVC, Take 2”,效果很好。

在我的 Globabl.asax 页面中,我定义了一些路由:

public static void RegisterRoutes(RouteCollection routes)
{
    // Routing config for the HelpDesk area
    routes.CreateArea("HelpDesk", "ProjectName.Areas.HelpDesk.Controllers",
        routes.MapRoute(null, "HelpDesk/{controller}/{action}", new { controller = "Ticket", action = "Index" }),
        routes.MapRoute(null, "HelpDesk/Ticket/Details/{TicketId}", new { controller = "Ticket", action = "Details", TicketId = "TicketId" })
    );
}

所以,如果我在浏览器地址栏中手动输入“http://localhost/HelpDesk/Ticket/Details/12”,我会得到我期望的结果。这是我的控制器:

public ActionResult Details(int TicketId)
{
    hd_Ticket ticket = ticketRepository.GetTicket(TicketId);
    if (ticket == null)
        return View("NotFound");
    else
        return View(ticket);
}

在我看来,我有:

<%= Html.ActionLink(item.Subject, "Details", new { item.TicketId } )%>

但该代码会生成“http://localhost/HelpDesk/Ticket/Details?TicketId=12”,它也会返回预期结果。我的问题是……

在使用 Steven Sanderson 的 Areas 时如何定义 ActionLink,它将创建一个干净的 URL,例如:“http://localhost/HelpDesk/Ticket/Details/12”?

【问题讨论】:

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


    【解决方案1】:

    试试

    <%= Html.ActionLink(item.Subject, "Details", new { TicketId = item.TicketId } )%>
    

    ActionLink 方法需要一个带有与参数名称匹配的键的字典。 (请注意,传递匿名对象是为了方便)。我相信它只会标记到 URL 的末尾。

    编辑:这对您不起作用的原因是因为您的第一个路由匹配并优先(控制器和操作),但未定义 TicketId 参数。您需要切换路线的顺序。您应该始终将最具体的路线放在首位。

    【讨论】:

      【解决方案2】:

      试试

      <%= Html.ActionLink(item.Subject, "Details", new { TicketId=item.TicketId } )%>
      

      【讨论】:

        【解决方案3】:

        我认为 Womp 有它......

        哦,当您交换路线时,请尝试

        routes.MapRoute(null, "HelpDesk/Ticket/Details/{TicketId}", new { controller = "Ticket", action = "Details"})
        

        我认为 TicketId = "id" 搞砸了

        希望对你有帮助,

        【讨论】:

          猜你喜欢
          • 2011-04-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多