11

So I've recently started with ASP.NET MVC 4. I'm using the razor engine. My question is concerning the view files, with suffix cshtml. It seems to me that these are precompiled into *.cs files by razor which in turn are then compiled into MSIL. (A pattern that is familiar from my days as a JSP developer.) One reason why I am making this assumption is that if I enter some invalid c# code into the cshtml file I get a compilation error displayed like this:

Line 34: public class _Page_Views_BaseDataAdmin_Index_cshtml : ...

And line 34 is not indicative of where the error is in the cshtml file, just as the class _Page_Views_BaseDataAdmin_Index_cshtml seems to refer to a regular .net class not the view file.

So my question is: Where do I find the cs file? Specifically, in the example above, "_Page_Views_BaseDataAdmin_Index_cshtml.cs"? Maybe I need to add some config to tell MVC to keep this .cs file on disk, if so, how do I do this?

Thanks!

Manolo
  • 1,597
  • 4
  • 21
  • 35
  • 1
    What is the reason behind wanting to know where these files are? For the most part you shouldn't need them for anything. – nerdybeardo Nov 02 '12 at 15:40
  • 2
    At the moment out of pedagogical reasons. The cognitive disposition of human beings is often such that knowledge/understanding is mostly built on top of pre-existing knowledge. And since I am more familiar with c# than with razor syntax it would help my understanding to view the generated c#. But I'm sure there are other reasons too. – Manolo Nov 02 '12 at 16:04
  • Other than educational curiosity, there is no reason to look at the generated .cs files. – jrummell Nov 02 '12 at 16:32
  • @jrummell except when Visual Studio reports (and even the ASP.NET error page) reports the wrong line number for your error - which is what just happened to me. although in that case I want to debug into it – Simon_Weaver Feb 18 '13 at 04:52
  • 1
    See also [ASP.NET Compilation Overview](http://msdn.microsoft.com/en-us/library/ms178466(v=vs.100).aspx) and [Understanding ASP.NET Dynamic Compilation](http://msdn.microsoft.com/en-us/library/ms366723(v=vs.100).aspx), which describe the entire process. From a Visual Studio perspective, [ASP.NET Web Application Project Precompilation Overview](http://msdn.microsoft.com/en-us/library/aa983464.aspx) has some good information, too. – Jarrod Dixon Sep 18 '13 at 22:42
  • Since one of the answers got an explanatory comment... Isn't the simplest course here to use the yellow screen of death to expand the source listing *and get the path to the view class*, since it's part of the error page output? – Tetsujin no Oni May 01 '14 at 13:31

2 Answers2

14

A quick tip to find the generated files is to add this to your .cshtml file.

This will show you immediately the full path

<div>This file is compiled to  @this.GetType().Assembly.CodeBase</div> 
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • Isn't this only going to help if the compilation will succeed? – Tetsujin no Oni Sep 30 '13 at 14:35
  • yes it must compile to find the initial folder, but if you're trying to debug a non-working page you can find the file you want by searching in this folder with 'find in files' to get the actual filename - something like `App_Web_5d1gwd4o.16.cs`. Just revert to a compilable version being careful not to lose changes. If you then update the `.cstml` file this same file will be updated and you can then examine it. – Simon_Weaver Apr 25 '14 at 19:52
6

You can find the compiled views in your Temporary ASP.NET Files folder.

First find the .compiled file that corresponds to your view (ex: home.cshtml): home.cshtml.a8d08dba.compiled

This file contains the assembly name: assembly="App_Web_hkc53urb"

Your .cs file will be the assembly name concatenated with a number: App_Web_hkc53urb.1.cs

An easier approach might be to use Windows search for your view name in the temp ASP.NET directory.

barry
  • 835
  • 6
  • 14