/Admin 区域中的 ASP.NET MVC 3 路由使用名称而不是 id
这个人一直困扰着我,所以我想我应该寻求一些帮助。我已经让它与我的“类别”路线一起工作,但由于某种原因,当我尝试对“消费者”做同样的事情时,它根本不起作用。这是必要的背景信息:
路由(AdminAreaRegistration.cs):
public override void RegisterArea(AreaRegistrationContext context)
{
//matches /Admin/BusinessCategories/MyCategory/children
context.MapRoute(
"ChildCategories",
"Admin/BusinessCategories/{category}/children",
new { controller = "BusinessCategories", action = "ViewChildren" }
);
//matches /Admin/BusinessCategories/MyCategory/edit
context.MapRoute(
"EditCategory",
"Admin/BusinessCategories/{category}/edit",
new { controller = "BusinessCategories", action = "Edit" }
);
// want this to match /Admin/Consumers/JoeBob/details
context.MapRoute(
"ConsumerDetails",
"Admin/Consumers/{alias}/details",
new { controller = "Consumers", action = "Details" }
);
//matches /Admin
//matches /Admin/BusinessCategories
//matches /Admin/BusinessCategories/New
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional },
new { id = @"\d+" }
);
}
然后在我的消费者控制器(ConsumersController.cs)中:
public class ConsumersController : Controller
{
public ActionResult Index()
{
...code...
}
[HttpGet]
public ActionResult Details(string alias)
{
return View(alias);
}
}
然后我像这样生成链接:
@Html.ActionLink(c.Alias, "Details", "Consumers", new { alias = c.Alias }, null)
问题是我对 @Html.ActionLink 的调用正在生成一个像这样的链接:
/Admin/Consumers/details?alias=JoeBob
(如果点击,将导致 404) 而不是我想要的,即:
/Admin/Consumers/JoeBob/details
我注意到的一件事是,如果我将 alias = c.Alias
更改为 id = c.Alias
,它会生成一个类似的 URL : /Admin/Consumers/details/JoeBob
(因此它从查询字符串中删除别名部分,但仍将“详细信息”放在别名之前。)
有什么想法吗?
编辑:重新启动 IIS 解决了我的问题。不知道为什么,但确实如此。
This one has been throwing me so I figured I'd ask for some help. I've got this to work with my "Categories" routes, but for some reason when I'm trying to do the same thing with "Consumers" it's not working at all. Here is the necessary background info:
Routing (AdminAreaRegistration.cs):
public override void RegisterArea(AreaRegistrationContext context)
{
//matches /Admin/BusinessCategories/MyCategory/children
context.MapRoute(
"ChildCategories",
"Admin/BusinessCategories/{category}/children",
new { controller = "BusinessCategories", action = "ViewChildren" }
);
//matches /Admin/BusinessCategories/MyCategory/edit
context.MapRoute(
"EditCategory",
"Admin/BusinessCategories/{category}/edit",
new { controller = "BusinessCategories", action = "Edit" }
);
// want this to match /Admin/Consumers/JoeBob/details
context.MapRoute(
"ConsumerDetails",
"Admin/Consumers/{alias}/details",
new { controller = "Consumers", action = "Details" }
);
//matches /Admin
//matches /Admin/BusinessCategories
//matches /Admin/BusinessCategories/New
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "AdminHome", action = "Index", id = UrlParameter.Optional },
new { id = @"\d+" }
);
}
Then in my Consumers controller (ConsumersController.cs):
public class ConsumersController : Controller
{
public ActionResult Index()
{
...code...
}
[HttpGet]
public ActionResult Details(string alias)
{
return View(alias);
}
}
Then I am generating my link like so:
@Html.ActionLink(c.Alias, "Details", "Consumers", new { alias = c.Alias }, null)
The problem is that my call to @Html.ActionLink is generating a link like:
/Admin/Consumers/details?alias=JoeBob
(which results in a 404, if clicked on)
rather than what I want, which is:
/Admin/Consumers/JoeBob/details
One thing I've noticed is if I change alias = c.Alias
to id = c.Alias
, it produces a URL like:/Admin/Consumers/details/JoeBob
(so it removes the alias part from the query string, but still puts 'details' before the alias.)
Any ideas?
Edit: Rebooting IIS solved the issue for me. Not sure why, but it did.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的路线配置正确(我测试过)。我怀疑您出现了这种不当行为,因为包含 @Html.ActionLink() 标记的视图位于您的管理区域之外。如果它位于您的管理区域内,您应该会看到生成的正确链接。但是,在您的管理区域之外,您需要声明您的目标区域,如下所示:
Your routes are configured properly (I tested them). I suspect that you're getting this misbehavior because the view that contains your @Html.ActionLink() mark-up is outside of your Admin Area. If it is inside your Admin Area, you should be seeing the proper link generated. However, outside of your Admin Area, you need to declare which area you're targeting like this: