gpt4 book ai didi

c# - ASP.NET RouteValueDictionary 包含带键 "RouteModels"的条目

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:16 25 4
gpt4 key购买 nike

在实现 WebApplication 时,我们在 RouteValueDictionary 中遇到了一个带有键“RouteModels”的可疑条目。

即使是 Google 也没有给我该条目的结果。有人可以解释它的来源吗?它来自自定义路由吗?或者这是受模型绑定(bind)约束的值?

Controller 中 ActionMethode 的声明是:

[Route("User/{user}")]
public ActionResult UserForums(User user, int page = 1)

请求的 RouteValueDictionary 包含以下条目:

enter image description here

最佳答案

Does it come from the custom routes?

不清楚。但是,路由值完全取决于自定义路由类的设计方式。

public class SampleRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var path = httpContext.Request.Path.Substring(1);
if (path.Equals("the/virtual/path"))
{
var routeData = new RouteData(this, new MvcRouteHandler());

routeData.Values["user"] = "TheUser";
routeData.Values["controller"] = "Home";
routeData.Values["action"] = "Custom";
routeData.Values["RouteModels"] = new List<RouteModel> { new RouteModel { Name = "Foo" }, new RouteModel { Name = "Bar" } };

routeData.DataTokens["MetaData"] = "SomeMetadata";

return routeData;
}

return null;
}

public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
string user = values["user"] as string;
string controller = values["controller"] as string;
string action = values["action"] as string;
IEnumerable<RouteModel> routeModels = values["RouteModels"] as IEnumerable<RouteModel>;

if ("TheUser".Equals(user) && "Home".Equals(controller) && "Custom".Equals(action))
{
// Use the route models to either complete the match or to do something else.


return new VirtualPathData(this, "the/virtual/path");
}

return null;
}

private class RouteModel
{
public string Name { get; set; }
}
}

我的猜测是,这要么是一些专门的路线,要么是 filter即在路由集合中存储额外数据。但是为什么作者选择将其存储为路由值而不是 DataTokens(用于元数据)是一个谜。

关于c# - ASP.NET RouteValueDictionary 包含带键 "RouteModels"的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36398668/

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