Generate T4MVC url inside ApiController
-
14-07-2021 - |
Question
Is there a way to generate a Url that is not part of an ApiController inside a ApiController using T4MVC? I dont have an option to do so. controller.Url
is null when I run the code below.
var controller = new UrlController();
foreach (var e in @event.Event.Reports)
{
e.Url = controller.Url.Action(EventReportTypes.GetTypes(e.ReportType, @event.Event.Id, AssetOutput.Html, false));
}
Solution
I got it to work by doing this.
var url = new UrlHelper(HttpContext.Current.Request.RequestContext);
if (url != null)
{
e.Url = url.ActionAbsolute(EventReportTypes.GetTypes(e.ReportType, @event.Id, AssetOutput.Html, false)).Split('&')[0];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow