问题描述
在我的应用我有控制器名为片段
无论是在默认区域(应用程序根目录),并在我的所谓的区域管理
。我用T4MVC和自定义路线,像这样的:
In my application I have controller named Snippets
both in default area (in application root) and in my area called Manage
. I use T4MVC and custom routes, like this:
routes.MapRoute(
"Feed",
"feed/",
MVC.Snippets.Rss()
);
和我得到这个错误:
多种类型的发现命名为片段控制器匹配。这可能发生,如果该服务此请求('{控制器} / {行动} / {ID} /')没有指定的命名空间的路线以搜索一个控制器的请求相匹配。如果是这种情况,通过调用'图路线的方法,需要一个命名空间参数的重载寄存器该路由。
有关'片断'请求发现下列匹配的控制器:
Snippets.Controllers.SnippetsController
Snippets.Areas.Manage.Controllers.SnippetsController
The request for 'snippets' has found the following matching controllers: Snippets.Controllers.SnippetsController Snippets.Areas.Manage.Controllers.SnippetsController
我知道有对图路线
重载采取命名空间
的说法,但目前还没有这样的过载与T4MVC支持。可能是我想的东西?可能的语法可以是:
I know that there are overloads for MapRoute
that take namespaces
argument, but there are no such overloads with T4MVC support. May be I'm missing something? The possible syntax can be:
routes.MapRoute(
"Feed",
"feed/",
MVC.Snippets.Rss(),
new string[] {"Snippets.Controllers"}
);
或者,它似乎很不错,我有命名空间T4MVC属性:
or, it seems quite good to me to have namespace as T4MVC property:
routes.MapRoute(
"Feed",
"feed/",
MVC.Snippets.Rss(),
new string[] {MVC.Snippets.Namespace}
);
在此先感谢!
推荐答案
有道理。我猜你只是碰上这第一个。尝试更换所有的图路线方法T4MVC.tt通过以下内容:
Makes sense. I guess you're just the first one to run into this. Try replacing all the MapRoute methods in T4MVC.tt by the following:
public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result) {
return MapRoute(routes, name, url, result, null /*namespaces*/);
}
public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, object defaults) {
return MapRoute(routes, name, url, result, defaults, null /*constraints*/, null /*namespaces*/);
}
public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, string[] namespaces) {
return MapRoute(routes, name, url, result, null /*defaults*/, namespaces);
}
public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, object defaults, object constraints) {
return MapRoute(routes, name, url, result, defaults, constraints, null /*namespaces*/);
}
public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, object defaults, string[] namespaces) {
return MapRoute(routes, name, url, result, defaults, null /*constraints*/, namespaces);
}
public static Route MapRoute(this RouteCollection routes, string name, string url, ActionResult result, object defaults, object constraints, string[] namespaces) {
// Start by adding the default values from the anonymous object (if any)
var routeValues = new RouteValueDictionary(defaults);
// Then add the Controller/Action names and the parameters from the call
foreach (var pair in result.GetRouteValueDictionary()) {
routeValues.Add(pair.Key, pair.Value);
}
var routeConstraints = new RouteValueDictionary(constraints);
// Create and add the route
var route = new Route(url, routeValues, routeConstraints, new MvcRouteHandler());
if (namespaces != null && namespaces.Length > 0) {
route.DataTokens = new RouteValueDictionary();
route.DataTokens["Namespaces"] = namespaces;
}
routes.Add(name, route);
return route;
}
请注意,你可以简单地通过写作获得的控制器命名空间强类型没有T4MVC的帮助:
Note that you can get strong typing on the controller namespace without T4MVC's help simply by writing:
string[] { typeof(MyApplication.Controllers.SnippetsController).Namespace }
我要补充一点,理想情况下,你就不必在所有通过命名空间,因为你的意图,以针对特定的控制器在MVC.Snippets.Rss()调用已被抓获。但是,我找不到一个明显的方法,使没有大的改变T4MVC这项工作。
I should add that ideally, you would not have to pass the Namespaces at all, since your intent to target a specific controller is already captured in the MVC.Snippets.Rss() call. However, I couldn't find an obvious way to make this work without big changes to T4MVC.
总之,请查看和测试的变化,让我知道它是如何为你工作。如果它看起来不错,我会得到它
Anyway, please review and test the change, and let me know how it works for you. If it looks good, I'll get it in.
谢谢!
这篇关于T4MVC并在不同的区域重复的控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!