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!

6
  • 1
    What is the reason behind wanting to know where these files are? For the most part you shouldn't need them for anything. Commented Nov 2, 2012 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. Commented Nov 2, 2012 at 16:04
  • Other than educational curiosity, there is no reason to look at the generated .cs files. Commented Nov 2, 2012 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 Commented Feb 18, 2013 at 4:52
  • 1
    See also ASP.NET Compilation Overview and Understanding ASP.NET Dynamic Compilation, which describe the entire process. From a Visual Studio perspective, ASP.NET Web Application Project Precompilation Overview has some good information, too. Commented Sep 18, 2013 at 22:42

2 Answers 2

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> 
Sign up to request clarification or add additional context in comments.

2 Comments

Isn't this only going to help if the compilation will succeed?
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.