How to use UrlHelper in signalR HubClass

I have a chat class that is issued from the Hub. I want to know if thers can create a URL by URLHelper, for example: Url.Action ("action", "controller").

how can I get a class from 2 abstract classes (Hub, Controller), I don’t know if another way can build full URls, and not Hard code.

+4
source share
2 answers

I am using this code in my hub currently, I am sure there is a better way to do this, but it works.

Note. If you want the full 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); } } 
+1
source

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)); 
-1
source

Source: https://habr.com/ru/post/1480957/


All Articles