locked
How to get page object in a hub RRS feed

  • Question

  • User-1094371921 posted

    This is my code,

        public class ChatHub : Microsoft.AspNet.SignalR.Hub
        {
            StringWriter sw = new StringWriter();        
    
            public void RefreshChat()
            {
                Page currentPage = Context.Request.GetHttpContext().Handler as Page;
                
                HtmlTextWriter writer = new HtmlTextWriter(sw);
                try
                {
                    MyCustomControl.RenderControl(writer);
                }
                catch (Exception e)
                {
    
                }
    
                Clients.Caller.RefreshChatContent(sw.ToString());
            }
    }

    The Page object currentPage is null in above code.

    I'm using hub to dynamically render a custom control. For this I need the page object, without the page object the custom control will throw exception and execution will be stopped.

    Update:

    My requirement is I need to share the URI of my resources (scripts, images and css for custom control) in server to client. If I have page object, I'll register the client scripts using it, since it is not available is there any other options to do it in a hub?

    Regards,

    Anandaraj

    Wednesday, March 25, 2015 4:16 AM

All replies

  • User1711366110 posted

    Hi Anand,
     As per your case, you can try like below :

    public class ChatHub : Microsoft.AspNet.SignalR.Hub
        {
            StringWriter sw = new StringWriter();        
    
            public void RefreshChat()
            {
                //Page currentPage = Context.Request.GetHttpContext().Handler as Page;
    
                Page currentPage=HttpContext.Current.Handler as Page;
                
                HtmlTextWriter writer = new HtmlTextWriter(sw);
                try
                {
                    MyCustomControl.RenderControl(writer);
                }
                catch (Exception e)
                {
    
                }
    
                Clients.Caller.RefreshChatContent(sw.ToString());
            }
    }

    --
    with regards,
    Edwin

    Thursday, March 26, 2015 1:25 AM
  • User-1094371921 posted

    @Edwin, Thanks for your reply, I already tried this, but handler is null in current context too.

    Thursday, March 26, 2015 10:23 AM