简体   繁体   中英

Get Action/Controller name from context

Can I get current Action/Controller name in SignalR hub like it is possible in usual MVC controllers using HttpContext ?

Currently I am using:

if (HttpContext.Current != null)
    return HttpContext.Current.Request.RawUrl;

But this only returns something like:

http://localhost:5226/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=...&connectionData=...name...xhub...name...yhub...&tid=1

Also the HubCallerContext does not seem to have further information.

Does anybody have an idea?

I just found out that I could use SignalR's Hub pipeline :

public class LoggingPipelineModule : HubPipelineModule 
{ 
    protected override bool OnBeforeIncoming(IHubIncomingInvokerContext context) 
    {
        Debug.WriteLine("Invoking '{0}.{1}({2})'.",
            context.MethodDescriptor.Hub.Name,
            context.MethodDescriptor.Name,
            string.Join(", ", context.Args));

        return base.OnBeforeIncoming(context); 
    }
}

That way I can set Action/Controller to a global object and access later on. This is of course not optimal.

You could try getting the values from the RouteData object

       var controller = HttpContext.Request.RequestContext.RouteData.Values["controller"].ToString();
       var action = HttpContext.Request.RequestContext.RouteData.Values["action"].ToString();

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM