Steve Konves
Steve Konves

Reputation: 2668

How to read the raw, unrendered contents of a cshtml file

I am creating a demo portion for an ASP.Net MVC website. I would like to be able to display the Razor markup of one of the page's partial views by clicking on a button on the page. To do this, I will have to fetch the Razor markup directly from the cshtml file and pass it to the view via the model.

How can this be done (specifically fetching the raw, unrendered razor markup)? I have attempted to open up a file stream to the partial view's cshtml file, but the file path to the views is not intuitive to me:

FileStream fs = new FileStream("{what goes here?}/Views/Demos/SomePartialView.cshtml", FileMode.Open );

Upvotes: 2

Views: 3684

Answers (1)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174369

Try Server.MapPath:

var localPath = Server.MapPath("~/Views/Demos/SomePartialView.cshtml");

Upvotes: 6

Related Questions