Asp.net mvc 2 MVC2从自定义属性中查找区域/控制器/操作

Asp.net mvc 2 MVC2从自定义属性中查找区域/控制器/操作,asp.net-mvc-2,Asp.net Mvc 2,我在两个单独的页面上使用部分视图,部分视图使用元数据以模型属性的形式获取显示名称(执行元数据的标准方式) 我需要使显示名称上下文敏感,具体取决于页面 为此,我扩展了System.ComponentModel.DisplayNameAttribute,并传入了area/controller/action/resourcefile/resourcestring数组,以便根据上下文选择正确的资源字符串 我的问题是如何从以下内容中获取区域/控制器/操作: using System; using Syst

我在两个单独的页面上使用部分视图,部分视图使用元数据以模型属性的形式获取显示名称(执行元数据的标准方式)

我需要使显示名称上下文敏感,具体取决于页面

为此,我扩展了System.ComponentModel.DisplayNameAttribute,并传入了area/controller/action/resourcefile/resourcestring数组,以便根据上下文选择正确的资源字符串

我的问题是如何从以下内容中获取区域/控制器/操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonInterfaces.Helpers;

namespace CommonInterfaces.ComponentModel
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class ContextSensitiveDisplayName : System.ComponentModel.DisplayNameAttribute
    {
        public class Context
        {
            public string Area { get; set; }
            public string Controller { get; set; }
            public string Action { get; set; }
            public Type ResourceType { get; set; }
            public string ResourceKey { get; set; }

            public Context(string area, string controller, string action, Type resourceType, string resourceKey)
            {
                this.Area = area;
                this.Controller = controller;
                this.Action = action;
                this.ResourceType = resourceType;
                this.ResourceKey = resourceKey;
            }
        }

        public ContextSensitiveDisplayName(params Context[] contexts)
        {
            /* Its these values that I need */
            string currentArea = ""; 
            string currentController = "";
            string currentAction = "";

            Context selectedContext = 
                contexts.FirstOrDefault(m => 
                    (m.Area == currentArea) && 
                    (m.Controller == currentController) && 
                    (m.Action == currentAction)
                );

            this.DisplayNameValue = ""; // Use the selectContext to retrieve string from resource file.
        }
    }
}

在此方面的任何帮助都将不胜感激

你没有。属性实例不是在知道路由的上下文中创建的。不能在属性中执行此操作

此信息也不是通过
DefaultModelBinder
传递给元数据提供程序的,因此编写自定义元数据提供程序不会有帮助,除非同时编写自定义模型绑定程序。这对我来说太多了


我建议使用不同的视图模型。

你不需要。属性实例不是在知道路由的上下文中创建的。不能在属性中执行此操作

此信息也不是通过
DefaultModelBinder
传递给元数据提供程序的,因此编写自定义元数据提供程序不会有帮助,除非同时编写自定义模型绑定程序。这对我来说太多了


我建议使用不同的视图模型。

这有点讨厌,但应该可以:

if(HttpContext.Current != null && HttpContext.Current.Handler is System.Web.Mvc.MvcHandler) 
{
    var handler = HttpContext.Current.Handler as System.Web.Mvc.MvcHandler;
    var controller = handler.RequestContext.RouteData.Values["controller"];
    var action = handler.RequestContext.RouteData.Values["action"];
}

这有点恶心,但应该有效:

if(HttpContext.Current != null && HttpContext.Current.Handler is System.Web.Mvc.MvcHandler) 
{
    var handler = HttpContext.Current.Handler as System.Web.Mvc.MvcHandler;
    var controller = handler.RequestContext.RouteData.Values["controller"];
    var action = handler.RequestContext.RouteData.Values["action"];
}

我最后用了这个

var routingValues = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)).Values;
  string currentArea = (string)routingValues["area"] ?? string.Empty;
  string currentController = (string)routingValues["controller"] ?? string.Empty;
  string currentAction = (string)routingValues["action"] ?? string.Empty;

在我将雅库布·科内基的答案标记为正确答案之前,我将尝试一下雅库布·科内基的答案——通过空检查和所有检查,他的答案看起来更加可靠。我很快就会谈到这个问题。

我最后用了这个

var routingValues = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current)).Values;
  string currentArea = (string)routingValues["area"] ?? string.Empty;
  string currentController = (string)routingValues["controller"] ?? string.Empty;
  string currentAction = (string)routingValues["action"] ?? string.Empty;

在我将雅库布·科内基的答案标记为正确答案之前,我将尝试一下雅库布·科内基的答案——通过空检查和所有检查,他的答案看起来更加可靠。我很快就会谈到这一点。

我记下这一点是因为你可以很容易地获得路由信息。我记下这一点是因为你可以很容易地获得路由信息。嗯,我同意“讨厌”的部分。:)嗯,我同意“讨厌”的部分