ASP.net MVC 区域并创建带有 ID 的 ActionLink(SEO/干净的 URL)

发布于 2024-08-01 20:44:49 字数 1752 浏览 4 评论 0原文

我正在使用 ASP.NET MVC 1.0 / C# 为客户构建帮助台票证系统。 我已经实现了 Steven Sanderson 的“应用程序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 的 将创建干净 URL 的区域,例如:“http ://localhost/HelpDesk/Ticket/Details/12”?

I am building a Help Desk Ticket system for a client using ASP.NET MVC 1.0 / C#. I have implemented Steven Sanderson's "App Areas in ASP.NET MVC, Take 2" and it is working great.

In my Globabl.asax page I have some routes defined as such:

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" })
    );
}

So, if I enter "http://localhost/HelpDesk/Ticket/Details/12" in the browser address bar manually, I get the results I expect. Here is my controller:

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

In my view I have:

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

But that code generates "http://localhost/HelpDesk/Ticket/Details?TicketId=12" which also returns the expected results. My Question is...

How do I define an ActionLink when using Steven Sanderson's Areas that will create a clean URL like: "http://localhost/HelpDesk/Ticket/Details/12" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨光如昨 2024-08-08 20:44:49

尝试

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

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

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

Try

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

The ActionLink method expects a dictionary with keys that match the parameter names. (Note that passing an anonymous object is a convenience for this). Anything else I believe it will just tag onto the end of the URL.

EDIT: The reason that this isn't working for you is because your first route matches and takes precedence (controller and action), but defines no TicketId parameter. You need to switch the order of your routes. You should always put your most specific routes first.

如梦初醒的夏天 2024-08-08 20:44:49

尝试

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

Try

<%= Html.ActionLink(item.Subject, "Details", new { TicketId=item.TicketId } )%>
走走停停 2024-08-08 20:44:49

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

哦,当你交换路线时尝试

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

我认为, TicketId = "id" 搞乱了事情

希望有帮助,

I think Womp has it ...

Oh and while you are swapping your routes try

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

I think the , TicketId = "id" is messing things up

Hope that helps,

Dan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文