如何将命名空间添加到自定义路由扩展

发布于 2024-09-30 22:07:57 字数 614 浏览 2 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱情眠于流年 2024-10-07 22:07:57

var dataTokens = new RouteValueDictionary();
var ns = new string[] {"MyProject.Controllers"};
dataTokens["Namespaces"] = ns;

routes.Add("Default", new CultureRoute(
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    null /*constraints*/,
    dataTokens
));


var dataTokens = new RouteValueDictionary();
var ns = new string[] {"MyProject.Controllers"};
dataTokens["Namespaces"] = ns;

routes.Add("Default", new CultureRoute(
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    null /*constraints*/,
    dataTokens
));

怪我入戏太深 2024-10-07 22:07:57

编辑:(之前创建了自定义路线,但这不是必需的)。这应该可以解决问题。至少在 MVC 4 中是这样,很可能在 MVC 3 中也是这样

Route defRoute = new CultureRoute ("{controller}/{action}/{id}",
            new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
            new SomeRouteHandler());
if( defRoute.DataTokens == null )
      defRoute.DataTokens = new RouteValueDictionary();
defRoute.DataTokens.Add("Namespaces", new string[] { "MVCApp.WebUI.Controllers" });
        routes.Add(defRoute);

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

Route defRoute = new CultureRoute ("{controller}/{action}/{id}",
            new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
            new SomeRouteHandler());
if( defRoute.DataTokens == null )
      defRoute.DataTokens = new RouteValueDictionary();
defRoute.DataTokens.Add("Namespaces", new string[] { "MVCApp.WebUI.Controllers" });
        routes.Add(defRoute);
牵你的手,一向走下去 2024-10-07 22:07:57

对于那些正在寻找解决方案的人:

您首先需要一个接受 DataTokens 参数的构造函数,并将其传递给 Route 构造函数。

例如,我使用的是在线获取的 DomainRoute 类,该类没有传递到 Domain 所需的附加参数。所以我简单地实现了一个类似于基本路由构造函数的构造函数:

public DomainRoute(string domain, string url, object defaults, object constraints, object dataTokens)
              : base(url, new RouteValueDictionary(defaults), new RouteValueDictionary(constraints),new RouteValueDictionary(dataTokens),new MvcRouteHandler())
{
              Domain = domain;
}

接下来,如果您已覆盖 GetRouteData 方法中,您必须在 RouteData 返回值中返回 DataToken。为了弄清楚这一点,我必须查看 Route.cs 源代码(谢谢 JAVA2S)。

RouteData data = new RouteData(...);
RouteValueDictionary dataTokens = DataTokens;
if (this.DataTokens != null) {
   RouteValueDictionary rdDataTokens = rd.DataTokens;
   foreach (var token in dataTokens)
      rdDataTokens.Add (token.Key, token.Value);
   }
}
return data;

现在只需按照 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:

public DomainRoute(string domain, string url, object defaults, object constraints, object dataTokens)
              : base(url, new RouteValueDictionary(defaults), new RouteValueDictionary(constraints),new RouteValueDictionary(dataTokens),new MvcRouteHandler())
{
              Domain = domain;
}

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).

RouteData data = new RouteData(...);
RouteValueDictionary dataTokens = DataTokens;
if (this.DataTokens != null) {
   RouteValueDictionary rdDataTokens = rd.DataTokens;
   foreach (var token in dataTokens)
      rdDataTokens.Add (token.Key, token.Value);
   }
}
return data;

Now simply put in your namespaces in dataTokens["Namespaces"] as per Fleents post.

Regards,
Daryl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文