【问题标题】:MVC Custom Route. DataTokens["Namespaces"] is ignoredMVC 自定义路由。 DataTokens["Namespaces"] 被忽略
【发布时间】:2014-04-11 19:20:14
【问题描述】:

我不知道为什么在创建继承自 RouteCustomRoute 时,DataTokens["Namespaces"] 字段被忽略。

我得到了错误: 找到了与名为“Home”的控制器匹配的多种类型。如果为该请求提供服务的路由 ('{action}/{id}') 未指定命名空间来搜索与请求匹配的控制器,则可能会发生这种情况。如果是这种情况,请通过调用采用“命名空间”参数的“MapRoute”方法的重载来注册此路由。 这是示例:

Application_Start()

 public static void RegisterRoutes(RouteCollection routes)
 {
        //Create dataTokens object
        var dataTokens = new RouteValueDictionary();
        var ns = new[] {"MvcDomainRouting.Controllers.Delivery" };
        dataTokens["Namespaces"] = ns;

        //Note is a custom route
        routes.Add("DomainRoute", new DomainRoute(
            domain:"delivery.md",                                            // Domain with parameters
            url:"{action}/{id}",                                             // URL with parameters
            defaults: new { controller = "Home", action = "Index", id = "" },// Parameter defaults
            constraints:null,
            dataTokens: dataTokens,
            routeHandler:new MvcRouteHandler()
        ));
}  

DomainRoute.cs

public class DomainRoute : Route
{
    public string Domain { get; set; }

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

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        //Details ommited

        // Route data
        RouteData data = new RouteData(this, RouteHandler);

        // 1.Add defaults
        // 2.Map URL key/values
        // Copy the DataTokens from the Route to the RouteData
        if (DataTokens != null)
        {
            foreach (var prop in DataTokens)
            {
                data.DataTokens[prop.Key] = prop.Value;
            }
        }

        return data;
        // At this point `data` holds the DataTokens["Namespaces"] see picture
    }

}

来自return data; 的堆栈跟踪:

HomeController.cs

namespace MvcDomainRouting.Controllers.Lunch
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}

namespace MvcDomainRouting.Controllers.Delivery
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return Content("Index din delivery");
        }

        public ActionResult About()
        {
            return Content("About din delivery");
        }
    }
}

提前感谢每一位分享经验的人。

如果需要更多详细信息,请询问。

【问题讨论】:

  • 如果在 dataTokens 中放置一个虚假的命名空间会发生什么?例如dataTokens["Namespaces"] = "Not.A.Namespace";
  • 不幸的是同样的事情..(

标签: c# asp.net-mvc asp.net-mvc-routing custom-routes


【解决方案1】:

问题是我正在创建 RouteValueDictionaryRouteValueDictionary

这里是固定版本:

像这样:

public DomainRoute( ...,object dataTokens,...)
    : base(...,dataTokens as RouteValueDictionary ,...)
//  instead of
//  : base(...,new RouteValueDictionary(dataTokens),..)
{
    Domain = domain;
}

或者像这样

        //....
        constraints:null,
        namespaces: new []{"MvcDomainRouting.Controllers.Delivery" },
        routeHandler:new MvcRouteHandler()
    ));

    public DomainRoute( ...,string[] namespaces,...)
    : base(...,new RouteValueDictionary(){{"Namespaces",namespaces}} ,...)
    {

    }  

【讨论】:

  • 感谢您提供答案,我昨天花了太多时间思考这个问题。
  • 不客气。谢谢你2试图解决它:)
猜你喜欢
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多