首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在WebApi请求中获取控制器名称?

如何在WebApi请求中获取控制器名称?
EN

Stack Overflow用户
提问于 2016-08-09 13:07:29
回答 3查看 13.8K关注 0票数 2

我有一个logging.cs类,用于在我的WebApi项目中记录有关传入请求的信息。

类是从我的Application_BeginRequest()方法在我的Global.asax中触发的。我需要在请求中获得控制器名称和授权头。怎么做?

我一直在玩HttpRequest请求对象,但我似乎无法准确地利用它是什么。(尽管Http请求的方法看起来很容易!)

对于授权头,我认为它将像“Request.Headers”授权一样简单;“但是,它现在返回为null。

我的代码如下,任何方向或建议都将不胜感激。-Jason

代码语言:javascript
运行
复制
namespace WCAPI.BLL
{
    public class logging_2
    {
        private static HttpRequest Request
        {
            get { return HttpContext.Current.Request; }
        }

        private static HttpResponse Response
        {
            get { return HttpContext.Current.Response; }
        }

        public static void LogWcapiRequest(BLL.DebugTmr tmr)
        {
            if (tmr.EventType == DebugTmr.EventTypes.Null) { return; }

            try
            {
                Services.AFSILog log = new Services.AFSILog();
                log.Level = Services.LogLevels.Info;
                log.SourceSystem = ANACore.AMTConfig.GetConfigurationValue("ConsoleLogSourceSystem");
                if (Request.Url.IsLoopback) { log.SourceSystem += "_" + System.Environment.MachineName; }
                log.Stamp = DateTime.Now;
                log.Message = tmr.FormatedMsg;
                log.Category = tmr.EventType.ToString();


                List<Services.LogData> dets = new List<Services.LogData>();
                dets.Add(new Services.LogData { DataType = Services.ParameterDataTypes.Int, DataKey = "Duration", DataValue = tmr.ElapsedMs.ToString() });

                //This appears to be easy!! 
                var meth = Request.HttpMethod;
                dets.Add(new Services.LogData { DataType = Services.ParameterDataTypes.Int, DataKey = "Method", DataValue = meth });

                //Now how do I get Authorization Header and Controller name ?


                foreach (BLL.DebugTmr.Waypoint wp in tmr.Waypoints)
                {
                    dets.Add(new Services.LogData
                    {
                        DataType = Services.ParameterDataTypes.Int,
                        DataKey = wp.Name + "Ms",
                        DataValue = wp.ElapsedMs.ToString()
                    });
                }

                log.Parameters = dets.ToArray();
                // This is what actually writes to the log I just need add Authorization header and Controller name to my log object
                SaveLog(log);
            }
            catch (Exception ex)
            {
                Debug.Print("Page log create failed : {0}", ex.Message);
            }
        }
    }
}
EN

回答 3

Stack Overflow用户

发布于 2016-08-09 13:34:25

您应该为logging.That实现一个操作筛选器,您将能够通过HttpActionContext参数访问控制器名称。

Api控制器:

代码语言:javascript
运行
复制
public class LeadsController : ApiController
{
    [Logger]
    public List<string> Get()
    {
        return new List<string> { "Lead 1", "Lead 2", "Lead 3", "Lead 4","Lead 5" };
    }
}

滤波器:

代码语言:javascript
运行
复制
public class Logger : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        string controllerName = 
            actionContext.ControllerContext.ControllerDescriptor.ControllerName;
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
    }
}

您可以装饰每个需要使用[Logger]进行日志记录的方法,或者在控制器级别上这样做,以记录在该controller.Lastly中发生的每一个调用,您可以将操作筛选器设置为全局,以便每当在项目中调用任何操作时,它都会运行。

票数 6
EN

Stack Overflow用户

发布于 2020-11-22 22:49:39

类似于Sin的答案,但对于ASP.Net Core3.1:

代码语言:javascript
运行
复制
var myControllerName = ControllerContext.ActionDescriptor.ControllerName;
票数 4
EN

Stack Overflow用户

发布于 2020-05-13 11:21:53

如果您需要在controller操作中获取控制器名称,则可以使用

this.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38851660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档