获取控制器名称 [英] Get controller name

查看:92
本文介绍了获取控制器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WebApiConfig.cs中,我有以下内容

In WebApiConfig.cs i have the following

public static void Register(HttpConfiguration config)
{

   config.MapHttpAttributeRoutes(); 

   config.Services.Replace(typeof(IHttpControllerSelector),
               new MyApiControllerSelector(config));

   //code omitted for brevity
}

然后在 MyApiControllerSelector.cs ,我想获取控制器

then in the MyApiControllerSelector.cs i want to get the controller

public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {           
            var routeData = request.GetRouteData();           

            var controllerName = (string)routeData.Values["controller"];

            //code omitted for brevity
        }

非常简单并且效果很好,但现在使用属性路由,我认为它需要一种不同的方法? -因为我似乎找不到简单的方法

Pretty simple and it worked great but now using attribute routing i think it needs a different approach? - as i can't seem to find a simple way

我尝试过

var controllerName = request.GetActionDescriptor().ControllerDescriptor.ControllerName;

这不起作用。

然后阅读并进行调试, request.GetRouteData()。Values [ MS_SubRoutes]

Then reading the source with debugging lead me to request.GetRouteData().Values["MS_SubRoutes"]

所以我现在有

string subRoutesKey = "MS_SubRoutes";

var attributedRoutesData = routeData.Values[subRoutesKey] as IEnumerable<IHttpRouteData>; 
var subRouteData = attributedRoutesData.FirstOrDefault();

var actions = (ReflectedHttpActionDescriptor[])subRouteData.Route.DataTokens["actions"];
var controllerName = actions[0].ControllerDescriptor.ControllerName;

这有效,但必须是更简单的方法吗?

which works but it has to be a simpler way?

更新

@KiranChalla问我的用例是什么,所以我要发布剩余的代码。
基本上我是在解析版本媒体类型 Accept:application / vnd.app。{resource} .v {version} + json 从请求中返回一个

@KiranChalla asked what's my use case so i'm posting the remaining code. Basically i'm parsing version media type Accept: application/vnd.app.{resource}.v{version}+json from request and returning a HttpControllerDescriptor depending on the version.

            HttpControllerDescriptor oldControllerDescriptor;
            if (controllers.TryGetValue(controllerName, out oldControllerDescriptor))
            {
                var apiVersion = GetVersionFromMediaType(request);

                var newControllerName = String.Concat(controllerName, "V", apiVersion);

                HttpControllerDescriptor newControllerDescriptor;
                if (controllers.TryGetValue(newControllerName, out newControllerDescriptor))
                {                    
                    return newControllerDescriptor;
                }               
                return oldControllerDescriptor;
            }
            return null;


推荐答案

如@KiranChalla所确认,没有比这更简单的方法了我已经实现的一个,除了次要建议使用 GetSubRoutes()

As confirmed by @KiranChalla there is no simpler way then the one I've already implemented, except the minor suggestion to use GetSubRoutes()

var attributedRoutesData = request.GetRouteData().GetSubRoutes();
var subRouteData = attributedRoutesData.FirstOrDefault();

var actions = (ReflectedHttpActionDescriptor[])subRouteData.Route.DataTokens["actions"];
var controllerName = actions[0].ControllerDescriptor.ControllerName;

这篇关于获取控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆