Reputation: 9085
When I have access to UrlHelper I can convert an ActionResult to a string (i.e. the actual URL) like this: urlHelper.RouteUrl(actionResult.GetRouteValueDictionary());
How can I do that same from a static method where I don't have access to UrlHelper? Thanks.
Upvotes: 4
Views: 4371
Reputation: 1269
I have rendered a partial result with a method like this.
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();
}
}
Upvotes: 0
Reputation: 180808
Just add a using statement for System.Web.Mvc, and create an instance of the UrlHelper class in your static method.
Upvotes: 6