How use UrlHelper in signalR HubClass

前端 未结 2 841
执念已碎
执念已碎 2021-01-18 10:09

I have a Chat class drived from Hub. I want to know if thers a way to built URL by URLHelper like : Url.Action(\"action\",\"Controller\").

as i can derived the cla

相关标签:
2条回答
  • 2021-01-18 10:38

    I use this code in my Hub currently, I'm sure there is a better way to do this, but this works.

    Note: If you want a a fully qualified url, make sure you set the domain correctly (example.com).

    protected virtual UrlHelper Url
    {
        get
        {
            var httpContext = HttpContext.Current;
    
            if (httpContext == null)
            {
                var request = new HttpRequest("/", "http://example.com", "");
                var response = new HttpResponse(new StringWriter());
                httpContext = new HttpContext(request, response);
            }
    
            var httpContextBase = new HttpContextWrapper(httpContext);
            var routeData = new RouteData();
            var requestContext = new RequestContext(httpContextBase, routeData);
    
            return new UrlHelper(requestContext);
        }
    }
    
    0 讨论(0)
  • 2021-01-18 10:43

    You can do it with this, and I am using T4MVC.

    var urlHelper = new UrlHelper(Context.Request.GetHttpContext().Request.RequestContext);
    var url = urlHelper.Action(MVC.Directors.EventSchedule.Index(EventId));
    
    0 讨论(0)
提交回复
热议问题