说得通。我猜你只是第一个遇到这种情况的人。尝试将 T4MVC.tt 中的所有 MapRoute 方法替换为以下内容:
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 帮助的情况下在控制器命名空间上获得强类型,只需编写:
string[] { typeof(MyApplication.Controllers.SnippetsController).Namespace }
我应该补充一点,理想情况下,您根本不必传递命名空间,因为您针对特定控制器的意图已经在 MVC.Snippets.Rss() 调用中捕获。但是,如果不对 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.
Thanks!