Convert ActionResult to URL string in static method

When I have access to UrlHelper I can convert the ActionResult to a string (i.e. the actual URL), for example: urlHelper.RouteUrl (actionResult.GetRouteValueDictionary ());

How can I do the same from a static method when I do not have access to UrlHelper? Thanks.

+3
source share
2 answers

Just add the using statement for System.Web.Mvc and instantiate the UrlHelper class in the static method.

+6
source

I got a partial result using this method.

private string RenderPartialViewToString(ControllerContext context, string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = context.RouteData.GetRequiredString("action");

        ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
            ViewContext viewContext = new ViewContext(context, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return sw.GetStringBuilder().ToString();
        }
    }
0
source

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


All Articles