Viewing Razor Generated Code


          Razor view engine is become very much popular when it was first released because it is much smaller and lighter in size. Also it is very easy to learn it. You can say ' write less, do more '. You can get start and learn more about Razor at here. Razor is easy to use but I have seen that some developers having problem of using Razor. They are getting compilation errors when they use Razor syntax. So in this article I will show you how can you correct your compilation errors by seeing the code generated by Razor view engine.



          First of all add a simple Razor view in your MVC project. Then add the following lines in your Razor view,


<html xmlns="">
<head runat="server">
    <title>My Razor View Page</title>
		@if(Model == null){
			<span>Model is null.</span>
			<span>Model is null.</span>



          Note above that I am using @Sorry. This is because I want to show you the ASP.NET Compilation Error screen, which you will also get when you have compilation errors in your view. Now run your application, you will see the following Compilation Error screen,




          Now just click on Show Complete Compilation Source link (which is shown above). Clicking this link will show you C# code of your view which is generated by Razor view engine.




          In the above screen you will find this, this.Write(Sorry). Since Sorry does not exist in this context, I mean Sorry is neither a property nor a field. So it is unknown to C# compiler. So that's why you are getting a compilation error. Seeing the C# code generated by Razor view engine make it easy for you to correct the compilation error.



          In this article I showed you how to correct your compilation errors when you are getting compilation errors using new Razor view engine. I also showed you that how to see the C# code generated by Razor view engine which helps you to make your syntax correct. Hopefully this will help you when you are having problem of using Razor, which is one of the new feature of ASP.NET MVC 3 (Preview 1). Hopefully you will enjoy this article too.

No Comments