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

标签 c# asp.net asp.net-mvc asp.net-mvc-5 model-binding

在实现 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/

相关文章:

c# - 如何检查 Windows 注册表在 .NET Core 应用程序中是否可用?

c# - 保存winform控件值c#

c# - 如何在 "website"模式下使用 ResourceManager?

c# - 通过 Web 服务自动公开数据库表

asp.net-mvc - AngularJS 服务器端变量到 ng-init 和方法序列

asp.net - 为WebForms开发人员学习MVC

asp.net-mvc - 从命令行在 IISExpress 中运行 ASP.NET

c# - Silverlight - C# 和 LINQ - 按多个字段排序

c# - 如何阻止用户在 session 事件期间单击屏幕上的任意位置

asp.net - EF 4.1 N Tier ASP .Net 困惑