在 ASP MVC 中处理复杂的 URL
我认为,我有一个复杂的 URL 需要在 ASP MVC 1.0 中处理: 我在大多数控制器中的所有操作始终需要两个参数:帐户和项目。 这是每个操作的要求之上的。 这意味着典型的 URL 如下所示:
http://abcd.com/myaccount/projects/project_id/sites/edit/12
在本例中: myaccount 是帐户名称。 项目可以是控制者,其他选项如位置、员工。 project_id 是 myaccount 中项目的 ID,sites 可以是控制器,其他选项如 staff 或付款。 edit 是一个操作,12 是所编辑网站的 ID。 (希望这足够清楚)
现在一种选择是创建一条路线,并通过向所有操作添加两个额外参数来将project_id 和帐户传递到控制器的所有操作中。 这并不是真正想要的,而且我不确定这两个控制器(项目和站点)是否会在这里工作。
我的理想情况是使用某种随调用控制器操作而移动的上下文,并将 project_id 和 myaccount 存储在其中。 然后可以按照正常方式处理其余参数,例如:
// sitescontroller
public ActionResult Edit(string id)
{
string account = somecontext["account"];
string project_id = somecontext["project"];
// do stuff
}
关于如何/在哪里发生这种情况有什么想法吗? 另外,这将如何与 ActionLink 一起使用(即根据此上下文生成正确的链接)?
谢谢!
I have - I think - a complex URL to deal with in ASP MVC 1.0:
All my actions in most of the controllers require two parameters all the time: Account and Project. This is on top of each Action's requirements. This means a typical URL is like this:
http://abcd.com/myaccount/projects/project_id/sites/edit/12
In this example:
myaccount is the account name. projects can be a controller, others options are like locations, employees. project_id is the id of a project within myaccount, sites could be a controller, other options are like staff or payments. edit is an action and 12 is the id of the site edited.
(hope this is clear enough)
Now one option is to create a route and pass project_id and account into all actions of controllers by adding two extra parameters to all actions. This is not really desired and also I'm not sure the two controllers (projects and sites) are going to work here.
My ideal situation is to use some kind of context that travels with the call to the controller action and store project_id and myaccount in there. The rest of the parameters then can be dealt with in a normal way like:
// sitescontroller
public ActionResult Edit(string id)
{
string account = somecontext["account"];
string project_id = somecontext["project"];
// do stuff
}
Any ideas as to how/where this can happen? Also how is this going to work with ActionLink (i.e. generating correct links based on this context)?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您首先需要将令牌添加到路由中,例如
{company}/projects/{project}{controller}/{action}/{id}
。 然后,如果您编写自己的 IControllerFactory,那么可以很容易地通过构造函数或您想要的方式将 RouteData 中的值推送到控制器中。 最简单的入门方法可能是继承 DefaultControllerFactory 并重写 CreateController 方法。You first need to add the tokens to your routes like
{company}/projects/{project}{controller}/{action}/{id}
. Then if you wrote your own IControllerFactory then it would be very easy to push the values from the RouteData into the controller via the constructor or however you wanted to do it. Probably the easiest way to get started would be to subclass DefaultControllerFactory and override the CreateController method.这对我来说不太有意义。 为什么你会有类似于以下的路线:
?
This doesn't quite make sense to me. Why would you have a route that is akin to the following:
?