如何将命名空间添加到自定义路由扩展
我使用routes.add而不是routes.maproute(它有一个命名空间arg) 因为我扩展了路线类。 我需要在路线上添加命名空间,因为我的一个区域具有相同的控制器名称 网站内。 我的问题是我不知道将命名空间放在哪里
public class CultureRoute : Route
{
public CultureRoute(string url, object defaults, object constraints, RouteValueDictionary dataTokens)
: base(url, new RouteValueDictionary(constraints), dataTokens, new MvcRouteHandler())
{
}
}
..Global.asax
routes.Add("Default", new CultureRoute(
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional}));
Im using routes.add instead of routes.maproute (which has a namespace arg)
because I extended the Route Class.
I need to add namespace on the routes because one of my Areas has the same controller name
within the site.
My problem is I dont know where to put the namespace..
public class CultureRoute : Route
{
public CultureRoute(string url, object defaults, object constraints, RouteValueDictionary dataTokens)
: base(url, new RouteValueDictionary(constraints), dataTokens, new MvcRouteHandler())
{
}
}
Global.asax
routes.Add("Default", new CultureRoute(
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional}));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:(之前创建了自定义路线,但这不是必需的)。这应该可以解决问题。至少在 MVC 4 中是这样,很可能在 MVC 3 中也是这样
EDIT: (Previously created a custom route but that wasn't necessary). This should do the trick. At least it does in MVC 4 and most probably MVC 3
对于那些正在寻找解决方案的人:
您首先需要一个接受 DataTokens 参数的构造函数,并将其传递给 Route 构造函数。
例如,我使用的是在线获取的 DomainRoute 类,该类没有传递到 Domain 所需的附加参数。所以我简单地实现了一个类似于基本路由构造函数的构造函数:
接下来,如果您已覆盖 GetRouteData 方法中,您必须在 RouteData 返回值中返回 DataToken。为了弄清楚这一点,我必须查看 Route.cs 源代码(谢谢 JAVA2S)。
现在只需按照 Fleents 帖子将命名空间放入 dataTokens["Namespaces"] 中即可。
问候,
达里尔
For those who were hunting for a solution for this:
You first need a constructor that accepts the DataTokens argument, and pass that through to the Route constructor.
For example, I was using a DomainRoute class I picked up online, which did not have the additional arguments required to pass through to Domain. So I simply implemented a constructor similar to the base Route constructor:
Next, if you have overriden your GetRouteData method, you must return the DataTokens in your RouteData return value. In order to figure this out, I had to look in the Route.cs source code (THANK YOU JAVA2S).
Now simply put in your namespaces in dataTokens["Namespaces"] as per Fleents post.
Regards,
Daryl