How to specify different Layouts in the ASP.NET MVC 3 razor ViewStart file? Wednesday, Dec 7 2011 

I would like to have 2 separate Layouts in my application. Let say one is for the Public section of the website and the other is for the Member side.

For simplicity lets say all the logic for each of theses sites is wrapped neatly into 2 distinct controllers.

  • PublicController
  • StaffController

And that they each have a corresponding Layout for all the View under each.

  • _PublicLayout.cshtml
  • _StaffLayout.cshtml

How do I use the _ViewStart.cshtml file to specify that all View’s / Action under “Public” use the PublicLayout and everything under “Staff” use the StaffLayout?


You could put a _ViewStart.cshtml file inside the /Views/Public folder which would override the default one in the /Views folder and specify the desired layout:

@{     Layout = "~/Views/Shared/_PublicLayout.cshtml"; } 

By analogy you could put another _ViewStart.cshtml file inside the /Views/Staff folder with:

@{     Layout = "~/Views/Shared/_StaffLayout.cshtml"; } 

You could also specify which layout should be used when returning a view inside a controller action but that’s per action:

return View("Index", "~/Views/Shared/_StaffLayout.cshtml", someViewModel); 

Yet another possibility is a custom action filter which would override the layout. As you can see many possibilities to achieve this. Up to you to choose which one fits best in your scenario.


Scaffolding – ASP.NET, NuGet, Entity Framework Code First and More Tuesday, Sep 6 2011 

Watch video by Steve here:

How to enumerate FormCollection in ASP.NET MVC? Wednesday, Aug 31 2011 

Here are 5 ways to do it specifically with a FormCollection object.

public ActionResult SomeActionMethod(FormCollection formCollection)
  foreach (var key in formCollection.AllKeys)
    var value = formCollection[key];

  foreach (var key in formCollection.Keys)
    var value = formCollection[key.ToString()];

  // Using the ValueProvider
  var valueProvider = formCollection.ToValueProvider();
  foreach (var key in valueProvider.Keys)
    var value = valueProvider[key];

foreach(var key in Request.Form.AllKeys)
   var value = Request.Form[key];
foreach(KeyValuePair<string, ValueProviderResult> kvp in form.ToValueProvider())
    string htmlControlName = kvp.Key;
    string htmlControlValue = kvp.Value.AttemptedValue;

C# Razor Syntax Quick Reference Tuesday, Aug 30 2011 

I gave a presentation to another team at Microsoft yesterday on ASP.NET MVC and the Razor view engine and someone asked if there was a reference for the Razor syntax.

It turns out, there is a pretty good guide about Razor available, but it’s focused on covering the basics of web programming using Razor and inline pages and not just the Razor syntax.

So I thought it might be handy to write up a a really concise quick reference about the Razor syntax.

Syntax/Sample Razor Web Forms Equivalent (or remarks)
Code Block
  int x = 123; 
  string y = "because.";
  int x = 123; 
  string y = "because."; 
Expression (Html Encoded)
<span><%: model.Message %></span>
Expression (Unencoded)
<span><%= model.Message %></span>
Combining Text and markup
@foreach(var item in items) {
<% foreach(var item in items) { %>
  <span><%: item.Prop %></span>
<% } %>
Mixing code and Plain text
@if (foo) {
  <text>Plain Text</text> 
<% if (foo) { %> 
  Plain Text 
<% } %>
Mixing code and plain text (alternate)
@if (foo) {
  @:Plain Text is @bar
Same as above
Email Addresses
Razor recognizes basic email format and is smart enough not to treat the @ as a code delimiter
Explicit Expression
In this case, we need to be explicit about the expression by using parentheses.
Escaping the @ sign
<span>In Razor, you use the 
@@foo to display the value 
of foo</span>
@@ renders a single @ in the response.
Server side Comment
This is a server side 
multiline comment 
This is a server side 
multiline comment
Calling generic method
Use parentheses to be explicit about what the expression is.
Creating a Razor Delegate
  Func<dynamic, object> b = 
@b("Bold this")
Generates a Func<T, HelperResult> that you can call from within Razor. See this blog post for more details.
Mixing expressions and text
Hello @title. @name.
Hello <%: title %>. <%: name %>.

Notice in the last example that Razor is smart enough to know that the ending period is a literal text punctuation and not meant to indicate that it’s trying to call a method or property of the expression.

Let me know if there are other examples you think should be placed in this guide. I hope you find this helpful.