ActionLink/RouteLink 基于 URL 而不是控制器
我有一个使用以下结构的控制器:
.com/Object/375
但是,当我访问特殊管理权限时,我也可以使用以下 URL
.com/Admin/Object/375
无论您是否在“管理”部分,我都使用相同的用户控件,但它们都指向相同的控制器对象。我需要链接来维护该 URL 结构,而不是尝试将管理员用户踢回对象控制器。我目前正在使用路线名称方法,其中这些是我的路线名称(在 global.asax 中):
"Admin/-Object"
"Object/-Object"
"Object-Object"
这些路线名称捕获以下路线:
Admin/Object, Admin/Object/555, Object, Object/323
然后我在路线链接中使用以下内容
Html.RouteLink(id, Request.Url.Segments[1] + "-Object", new { id = id })
这工作正常,但有一种奇怪的气味 -还有其他想法吗?
澄清一下:我需要根据当前 URL 结构(有或没有管理员)正确创建 URL,并且路由将指向正确的控制器(两个 URL 相同),并且管理员特定内容将被注入到仅在“管理”部分中的页面(基于 URL)。
I have a controller that uses the following structure:
.com/Object/375
However, I can also use the following URL when I am accessing special admin rights
.com/Admin/Object/375
I use the same user controls whether you're in the Admin section or not, but they both point to the same Controller Object. I need for the links to maintain that URL structure and not try to kick an Admin user back to the Object controller. I am currently using the route name method, where these are my route names (in global.asax):
"Admin/-Object"
"Object/-Object"
"Object-Object"
These route names catch the following routes:
Admin/Object, Admin/Object/555, Object, Object/323
I then use the following in a route link
Html.RouteLink(id, Request.Url.Segments[1] + "-Object", new { id = id })
This works just fine, but has an odd smell - any other ideas?
To clarify: I need the URL to be properly created based on the current URL structure (with or without the Admin) and the routing will point to the correct controller (the same for both URLs) and the admin specific content will be injected into the page only if in the Admin section (based on URL).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
总而言之,使用 ViewBag 可能是一个更好的主意,因为使用 URL 段可能会导致意外错误,特别是在移动控件或视图时。
Just to wrap this up, using ViewBag is probably a better idea because using the URL segment might result in unexpected errors, especialy if you move the controls or views around.