0
public string GetRazorViewAsString(string filePath, object model = null)
{
        var resultString = new StringWriter();

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

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

        // Rebdering 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.

Is there any way of creating HTML by taking full path of the view

Srinivas M
  • 303
  • 1
  • 3
  • 12
  • If you look at the MVC package in NuGet, you'll see that it depends on a Razor package. If you look at that Razor package, you'll see that another project depends on it, and that project is capable of doing what you want. Do a little research and I think you will find it. – mason Dec 02 '15 at 13:50
  • Sure @mason. But currently running out of the time. So, checking if anyone faced the same kind of thing. So it would be faster. and also there is no dependencies for Microsoft.AspNet.Razor as i see in NuGet – Srinivas M Dec 02 '15 at 14:06
  • Just because you're running short on time doesn't mean you can't do adequate research. I think you'll find what you're looking for if you Google "asp.net razor to html library" – mason Dec 02 '15 at 14:11
  • yes..we are already doing it – Srinivas M Dec 02 '15 at 14:17

1 Answers1

0

You can implement a VirtualPathProvider. Create a class that inherits from VirtualPathProvider, then override two methods:

  • FileExists
  • GetFile

These methods accept a virtual path argument, which you can then map to some location on disk. Your controllers will be unaware that this provider exists (they continue to use virtual paths for views). You may need to also implement a VirtualFile.

For more information, see http://www.umbraworks.net/bl0g/rebuildall/2009/11/17/ASP_NET_MVC_and_virtual_views. This blog post is sourcing views from a database, but you can adapt it to source views from anywhere.