Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public string GetRazorViewAsString(string filePath, object model = null)
        var resultString = new StringWriter();

        var context = new HttpContextWrapper(HttpContext.Current);
        var routeData = new RouteData();

        // Creating the controller context
        var controllerContext = new ControllerContext(new RequestContext(context, routeData), new DummyController());

        // Rendering the view and getting the html to resultString
        var razor = new RazorView(controllerContext, filePath, null, false, null);
        razor.Render(new ViewContext(controllerContext, razor, new ViewDataDictionary(model), new TempDataDictionary(), resultString), resultString);

        // Returning the generated html
        return resultString.ToString();

public class DummyController : Controller { }

Currently, we are using above code for generating HTML for a view. In that, view path is a virtual path.

Now, we are planning to move the views outside of the project. So keeping virtual path is not possible now.

Any help on getting HTML by taking the full path of the view.
Updated 2-Dec-15 2:38am

1 solution

You can't. The views must be inside the project. The code will not have access to anything outside of the folder it's running from.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900