ASP.net MVC 区域并创建带有 ID 的 ActionLink(SEO/干净的 URL)
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
ActionLink 方法需要一个包含与参数名称匹配的键的字典。 (请注意,传递匿名对象对此很方便)。 我相信它只会标记到 URL 的末尾。
编辑:这对您不起作用的原因是您的第一个路由匹配并优先(控制器和操作),但没有定义 TicketId 参数。 您需要切换路线的顺序。 您应该始终将最具体的路线放在第一位。
Try
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.
尝试
Try
我认为 Womp 有它......
哦,当你交换路线时尝试
我认为,
TicketId = "id"
搞乱了事情希望有帮助,
丹
I think Womp has it ...
Oh and while you are swapping your routes try
I think the ,
TicketId = "id"
is messing things upHope that helps,
Dan