ASP.NET MVC 3 Beta Support for Code Runs before Views and Strongly Typed Models in Razor Views

Code that Runs Before Views Run

In ASP.NET MVC 3 Beta, we can add a file named _viewstart.cshtml in the views directory  and this will be shared among the all views with in the Views directory. The below code in the _viewstart.cshtml, sets the Layout page for every Views in the Views folder

 

  1. @{
  2.     Layout = "~/Views/Shared/_Layout.cshtml";
  3. }

 

You can put a _viewstart.cshtml file on the any subfolders of Views folder and it will be shared only for that sub folder.

 

Specify Strongly Typed Models in Razor Views

In ASP.NET MVC 3 Beta, we can simply specify the strongly typed model using the following syntax

  1. @model Models.RegisterModel

 

In the above syntax, we have specified RegisterModel in the Models namespace as strongly typed model for our Razor View.

Published Thursday, October 07, 2010 5:42 AM by shiju

Comments

# The Morning Brew - Chris Alcock » The Morning Brew #703

Friday, October 08, 2010 3:32 AM by The Morning Brew - Chris Alcock » The Morning Brew #703

Pingback from  The Morning Brew - Chris Alcock  » The Morning Brew #703

# Twitter Trackbacks for ASP.NET MVC 3 Beta Support for Code Runs before Views and Strongly Typed Models in Razor Views - Shiju Varghese's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 ASP.NET MVC 3 Beta Support for Code Runs before Views and Strongly Typed Models in Razor Views - Shiju Varghese's Blog         [asp.net]        on Topsy.com

# ASP.NET MVC 3 Beta Support for Code Runs before Views and Strongly Typed Models in Razor Views - Shiju Varghese's Blog

Tuesday, October 12, 2010 3:34 AM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# re: ASP.NET MVC 3 Beta Support for Code Runs before Views and Strongly Typed Models in Razor Views

Friday, October 29, 2010 9:33 AM by Igorbek

I found 2 bugs in Razor view-engine.

1. I can't write any inline-templates in _ViewStart, cause

WriteLitralTo is not defined in ViewStartPage class

2. Translator inserts unnecessary whitespace writes

For ex:

_ViewStart.cshtml

@{

Func<int, HelperResult> intFormatter = @<text>- @item -</text>;

}

That code translates to:

public class _ViewStart_cshtml : System.Web.Mvc.ViewStartPage {

...

public override void Execute() {

Func<int, HelperResult> intFormatter =item => new

System.Web.WebPages.HelperResult(__razor_template_writer => {

 WriteLiteralTo(@__razor_template_writer, " ");

 WriteLiteralTo(@__razor_template_writer, "- ");

                WriteTo(@__razor_template_writer, item);

 WriteLiteralTo(@__razor_template_writer, " -");

         }

     }

 }

So:

(1)

CS0103: The name 'WriteLiteralTo' does not exist in the current context

WriteLiteralTo is defined in System.Web.WebPages.WebPageBase, but

StartPage does not inherited from it.

(2)

Why these unnecessary WriteLiteralTo calls (" ")? - there is no in my

code. And these unnecessery calls occurs all the time even in other

views! Why? I don't need them!

If I change the code, by replacing single space by other spaces (for

ex, three tabs) between = and @:

Func<int, HelperResult> intFormatter =                  @<text>- @item -</text>;

translator generates:

WriteLiteralTo(@__razor_template_writer, "\t\t\t");

WriteLiteralTo(@__razor_template_writer, "- ");

...

so, it generates writes for whitespaces BEFORE @, from code context!

why? Is this bug?

Is this behaivor will be changed to release?

How to decide these problems?

Leave a Comment

(required) 
(required) 
(optional)
(required)