gpt4 book ai didi

c# - MVC 自定义路由。 DataTokens ["Namespaces"] 被忽略

转载 作者:行者123 更新时间:2023-11-30 16:13:01 26 4
gpt4 key购买 nike

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

我得到错误:找到与名为“Home”的 Controller 匹配的多种类型。如果为该请求提供服务的路由 ('{action}/{id}') 未指定 namespace 来搜索与请求匹配的 Controller ,则可能会发生这种情况。如果是这种情况,请通过调用采用“命名空间”参数的“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
}

}

返回数据; 的堆栈跟踪: enter image description here

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");
}
}
}

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

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

最佳答案

问题是我正在创建 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}} ,...)
{

}

关于c# - MVC 自定义路由。 DataTokens ["Namespaces"] 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22270511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com