+ All Categories
Home > Documents > Layouts With Razor MVC

Layouts With Razor MVC

Date post: 22-Oct-2015
Category:
Upload: mohit-vaidh
View: 46 times
Download: 2 times
Share this document with a friend
Description:
MVC Layouots in RAZOR
Popular Tags:
21
10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 1/21 ASP.NET MVC 3: Layouts with Razor Two weeks ago we shipped the ASP.NET MVC 3 Beta Release . It supports “go live” deployments, and includes a bunch of nice improvements/enhancements. You can see a summary of the new ASP.NET MVC 3 features in my beta announcement post . Also read my original ASP.NET MVC 3 Preview post to learn about other ASP.NET MVC 3 features that showed up with that initial preview release. This is another in a series of “mini-posts” I’m doing that talk about a few of the new ASP.NET MVC 3 Beta features in more detail: New @model keyword in Razor (Oct 22nd) Layouts (this post) In today’s post I’m going to discuss layout pages with Razor, and discuss some of the improvements to them that we introduced with the recent ASP.NET MVC 3 Beta. Razor Basics ASP.NET MVC 3 ships with a new view-engine option called “Razor” (in addition to continuing to support/enhance the existing .aspx view engine). You can learn more about Razor, why we are introducing it, and the syntax it supports from my Introducing Razor blog post. If you haven’t read that post yet, take a few minutes and read it now (since the rest of this post will assume you have read it). Once you’ve read the Introducing Razor post, also read my ASP.NET MVC 3 Preview post and look over the ASP.NET MVC 3 Razor sample I included in it. What are Layouts? You typically want to maintain a consistent look and feel across all of the pages within your web-site/application. ASP.NET 2.0 introduced the concept of “master pages” which helps enable this when using .aspx based pages or templates. Razor also supports this concept with a feature called “layouts” – which allow you to define a common site template, and then inherit its look and feel across all the views/pages on your site. Using Layouts with Razor In my last blog post I walked through a simple example of how to implement a /Products URL that renders a list of product categories:
Transcript
Page 1: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 1/21

ASP.NET MVC 3: Layouts with Razor

Two weeks ago we shipped the ASP.NET MVC 3 Beta Release. It supports “go live” deployments, and includes abunch of nice improvements/enhancements. You can see a summary of the new ASP.NET MVC 3 features in mybeta announcement post. Also read my original ASP.NET MVC 3 Preview post to learn about other ASP.NET MVC3 features that showed up with that initial preview release.

This is another in a series of “mini-posts” I’m doing that talk about a few of the new ASP.NET MVC 3 Beta featuresin more detail:

New @model keyword in Razor (Oct 22nd)Layouts (this post)

In today’s post I’m going to discuss layout pages with Razor, and discuss some of the improvements to them thatwe introduced with the recent ASP.NET MVC 3 Beta.

Razor Basics

ASP.NET MVC 3 ships with a new view-engine option called “Razor” (in addition to continuing to support/enhancethe existing .aspx view engine).

You can learn more about Razor, why we are introducing it, and the syntax it supports from my Introducing Razorblog post. If you haven’t read that post yet, take a few minutes and read it now (since the rest of this post willassume you have read it). Once you’ve read the Introducing Razor post, also read my ASP.NET MVC 3 Preview postand look over the ASP.NET MVC 3 Razor sample I included in it.

What are Layouts?

You typically want to maintain a consistent look and feel across all of the pages within your web-site/application. ASP.NET 2.0 introduced the concept of “master pages” which helps enable this when using .aspx based pages ortemplates. Razor also supports this concept with a feature called “layouts” – which allow you to define a commonsite template, and then inherit its look and feel across all the views/pages on your site.

Using Layouts with Razor

In my last blog post I walked through a simple example of how to implement a /Products URL that renders a list ofproduct categories:

Page 2: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 2/21

Below is a simple ProductsController implementation that implements the /Products URL above. It retrieves a list ofproduct categories from a database, and then passes them off to a view file to render an appropriate HTML responseback to the browser:

Here is what the Index.cshtml view file (implemented using Razor) looks like:

Page 3: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 3/21

The above view file does not yet use a layout page – which means that as we add additional URLs and pages to thesite we’ll end up duplicating our core site layout in multiple places. Using a layout will allow us to avoid thisduplication and make it much easier to manage our site design going forward. Let’s update our sample to use onenow.

Refactoring to use a Layout

Razor makes it really easy to start from an existing page and refactor it to use a layout. Let’s do that with oursimple sample above. Our first step will be to add a “SiteLayout.cshtml” file to our project under the \Views\Sharedfolder of our project (which is the default place where common view files/templates go):

Page 4: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 4/21

SiteLayout.cshtml

We’ll use the SiteLayout.cshtml file to define the common content of our site. Below is an example of what it mightlook like:

Page 5: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 5/21

A couple of things to note with the above file:

It is no longer necessary (as of the ASP.NET MVC 3 Beta) to have an @inherits directive at the top of the file.You can optionally still have one if you want (for example: if you want a custom base class), but it isn’trequired. This helps keep the file nice and clean. It also makes it easier to have designers and non-developers work on the file and not get confused about concepts they don’t understand.

We are calling the @RenderBody() method within the layout file above to indicate where we want the viewsbased on this layout to “fill in” their core content at that location in the HTML.

We are outputting the “View.Title” property within the <title> element of our <head> section. I’ll discuss howthis is used in a bit.

And now we have a common layout template that we can use to maintain a consistent look and feel across anynumber of pages on our site.

Index.cshtml

Let’s now update our Index.cshtml view to be based on the SiteLayout.cshtml file we just created. Below is an first-cut of what it might look like:

A couple of things to note about the above file:

We did not need to wrap our main body content within a tag or element – by default Razor will automaticallytreat the content of Index.cshtml as the “body” section of the layout page. We can optionally define “namedsections” if our layout has multiple replaceable regions. But Razor makes the 90% case (where you onlyhave a body section) super clean and simple.

We are programmatically setting the View.Title value within our Index.cshtml page above. The code within ourIndex.cshtml file will run before the SiteLayout.cshtml code runs – and so we can write view code thatprogrammatically sets values we want to pass to our layout to render. This is particularly useful for things likesetting the page’s title, as well as <meta> elements within the <head> for search engine optimization (SEO).

At the moment, we are programmatically setting the Layout template to use within our Index.cshtml page. We can do this by setting the Layout property on the View (note: in the first preview this property was called“LayoutPage” – we changed it to just be “Layout” with the ASP.NET MVC 3 Beta). I’ll discuss somealternative ways (new with the ASP.NET MVC 3 Beta) that we can set this property shortly.

And now when we request the /Products URL within our site, we’ll get the following HTML returned:

Page 6: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 6/21

Notice above how the returned HTML content is a merge of the SiteLayout.cshtmk and Index.cshtml. The “ProductCategories” title at the top is set correctly based on the view, and our dynamic list of categories is populated in thecorrect place.

DRYing things up with _ViewStart

Currently we are programmatically setting the layout file to use at the top of our Index.cshtml file. This is fine forcases where we have some view-specific logic where the layout file will vary depending on the specific view. Butsetting it this way can end up being redundant and duplicative for most web applications – where either all of theviews use the same layout, or if they have different layouts (for example: for mobile devices or localized sites) thelogic for which layout to pick is common across all of the views.

The good news is that Razor (starting with the ASP.NET MVC 3 Beta release) includes a new feature that enablesus to remove the need to explicitly set the Layout in each view – and instead allows us to define the layout logiconce for all views in the site – making our view files even cleaner and more maintainable (and ensuring we keep tothe DRY principle: Don’t Repeat Yourself):

Page 7: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 7/21

Starting with the ASP.NET MVC 3 Beta release, you can now add a file called _ViewStart.cshtml (or_ViewStart.vbhtml for VB) underneath the \Views folder of your project:

The _ViewStart file can be used to define common view code that you want to execute at the start of each View’srendering. For example, we could write code like below within our _ViewStart.cshtml file to programmatically set theLayout property for each View to be the SiteLayout.cshtml file by default:

Page 8: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 8/21

Because this code executes at the start of each View, we no longer need to explicitly set the Layout in any of ourindividual view files (except if we wanted to override the default value above).

Important: Because the _ViewStart.cshtml allows us to write code, we can optionally make our Layout selectionlogic richer than just a basic property set. For example: we could vary the Layout template that we use dependingon what type of device is accessing the site – and have a phone or tablet optimized layout for those devices, and adesktop optimized layout for PCs/Laptops. Or if we were building a CMS system or common shared app that isused across multiple customers we could select different layouts to use depending on the customer (or their role)when accessing the site.

This enables a lot of UI flexibility. It also allows you to more easily write view logic once, and avoid repeating it inmultiple places.

Note: You can also specify layouts within a Controller or an Action Filter. So if you prefer to keep thelayout selection logic there you can do that as well.

Finished Sample

Below is a screen-shot of the simple application we have been building:

Here is the ProductsControllers that implements the /Products URL and retrieves the categories from a databaseand passes them to a view template to render:

Page 9: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 9/21

Here is the Index.cshtml view that we are using the render the /Products response:

Here is the SiteLayout.cshtml layout file we are using to implement a consistent look and feel across our site:

Page 10: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 10/21

Here is the _ViewStart.cshtml file that we are using to specify that all views in our site by default use theSiteLayout.cshtml file:

And here is the generated HTML from the /Products URL:

Page 11: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 11/21

And because we now have a common layout file for our site, we can build out more functionality, controllers andviews within our application - and have a site UI experience that is both consistent and very easy to maintain.

More Advanced Stuff

Two common questions people often ask are:

1) Can I have nested layout files?

2) Can I have multiple, non-contiguous, replaceable regions within a layout file – so that I can “fill in” multiple differentsections within my view files.

The answer to both of these questions is YES! I’ll blog some samples of how to do this in the future.

Summary

As I’ve mentioned before, one of the themes we’ve focused on with the ASP.NET MVC 3 and Razor releases hasbeen to make the code you write cleaner and more concise. We think the new layout functionality coming with thisrelease contributes nicely towards making view files even easier to read and write. I’ll be covering other niceimprovements like this that are new to the ASP.NET MVC 3 Beta in future posts.

Hope this helps,

Page 12: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 12/21

Scott

P.S. In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at:twitter.com/scottgu

Published Friday, October 22, 2010 3:05 PM by ScottGu

Filed under: ASP.NET, .NET, MVC

Comments

# re: ASP.NET MVC 3: Layouts with Razor

Friday, October 22, 2010 7:07 PM by Jesse Brown

Looks great.

For legacy sites with webforms and MVC mix, the current view engine allowed us to use the webforms master page as themaster page for our MVC views. Is there any chance razor will support something similar and work with existing masterpages from the MVC/Webforms engine?

# re: ASP.NET MVC 3: Layouts with Razor

Friday, October 22, 2010 11:55 PM by allentranks

Great! Is there any difference between this and MasterPage? I mean, are there any features that Layout has whileMasterPage has not?

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 9:45 AM by Kevin Ortman

Thanks for another great post, Scott.

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 10:27 AM by mr. chopper

I'm a bit confused about the third picture - what does the "@model IList" bit do? Is this part of rendering the template (forwant of a better word, seeing as it looks quiet similar to JQuery Templating)? Sorry for the durrs to those who are used tothis.

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 10:30 AM by mr. chopper

Forget it. I didn't see the previous blog explaining this... It's still confusing the bejesus out of me, mind.

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 11:05 AM by Erx

Will we be able to return multiple views in one call in mvc3 final or will that feature be pushed out for mvc4?

Thanks

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 12:28 PM by Snehal

Hi scott

Page 13: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 13/21

First of thanks a lot for keeping us posted on new stuff we love you :).

I want to understand why are we bringing Layouts when we already have master pages?

or when should we use master page and when to go for layouts?

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 1:01 PM by Koistya `Navin

You're using a <!DOCTYPE html> doctype from HTML 5 in default view pages. How about replacing semantics of thosepage according to HTML5 spec as well. <header> instead of <div id="header">, <nav> instead of <div id="menu"> etc.

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 2:14 PM by Yos

Hi, with Razor, are you able to regions that can optionally be overwritten? As you are aware, in the aspx/masterpage model,you could define a placeholder that had content which was rendered if it wasn't overwritten. However, if you defined a thatregion in the aspx it would overwrite whatever was defined in the master. This has been very useful for me, and I waswondering if this functionality is possible in Razor?

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 2:51 PM by Akaka

Scott, Is it possible to test the view page in case we use _ViewStart.cshtml ?

Or just put the layout selection logic in controller to more easily testing the selection layout logic.

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 7:41 PM by Carlos Martinez

Hi there, this question may sound a bit stupid but.. will it be possible, some day in the future, to use razor with WebForms?

I don't want to use MVC because I hate having to create controllers for just about everything.. I prefer the logic to create"pages" when I need multiple pages in my website instead of actions!..

However, I like the simplicity of razor to generate dynamic content. So I would like to combine it with webForms.

I hope it makes sense.

Thanks

PS: When will Visual Studio support .cshtml and .vbhtml formatting and intellisense? Is it something planned for this year?

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 23, 2010 11:48 PM by Haa

_Viewstart.cshtml screws up things for if you want to render partial views using Ajax.

# re: ASP.NET MVC 3: Layouts with Razor

Sunday, October 24, 2010 10:14 AM by Brian Vallelunga

Have you considered add directive of @layout like @model to further simplify the code? Having to open a code block seemsneedless.

# re: ASP.NET MVC 3: Layouts with Razor

Sunday, October 24, 2010 12:15 PM by Joe

I have upgraded a MVC 2 Website to MVC 3 Website, and have switched over to using Razor and updated my corresponding

Page 14: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 14/21

views as a result. I am running into a problem where my jQuery ready function is not executing. Everything is rendering fine,

but none of my javascript code is running? What is going on?

# re: ASP.NET MVC 3: Layouts with Razor

Sunday, October 24, 2010 3:15 PM by marko

Bringing this into VS should be a priority, but unfortunately it is not and obviously Microsoft is targeting at weekenddevelopers with this when it should target and focus on professional developers and teams. And I do agree with Mike thatthis is going too fast and if launched with this pace the end product will again lack some features that will be exposed atStack overflow or similar sites in the first few days after the release.

Actually it would make sense if the ASP.NET MVC team would gather some feedback from the community as the whole andnot just from the few exposed personalities because that would make the whole MVC picture much more complete and wellthought. As it is now it's nothing more than just releasing new and new features one after the other.

# re: ASP.NET MVC 3: Layouts with Razor

Sunday, October 24, 2010 4:15 PM by DalSoft

Looks great masterpages with there runat="server" always looked out of place in a MVC view. However _ViewStart concernsme looks like a step back to code behind pages and seems to have the potential to break separation of concerns. I haven'tmade my mind up whether I like _ViewStart or not, think I need more persuading.

# re: ASP.NET MVC 3: Layouts with Razor

Sunday, October 24, 2010 5:39 PM by Tim Murphy

You have taken the razor to html/code spaghetti of past :-) Love the look of Razor. Just need to find some time to play with itmyself.

# re: ASP.NET MVC 3: Layouts with Razor

Sunday, October 24, 2010 10:50 PM by Eber I

so if I add a _ViewStart.cshtml file under Views/Products will it apply only to the views under Products?

# re: ASP.NET MVC 3: Layouts with Razor

Sunday, October 24, 2010 11:07 PM by MartinF

This seems nice.

What about areas and the location of the _ViewStart.cshtml file ?

Will it pick up the _ViewStart.cshtml file from the View folder (/Areas/SomeArea/Views/...) of an Area automatically and if notdefined look in /Views/ (in the root) ?

# re: ASP.NET MVC 3: Layouts with Razor

Monday, October 25, 2010 12:56 AM by Nuri

I don't think I've been this excited about web programming since ASP.NET 1.0! As soon as MVC3 Intellisense is released,I'm there... We're so proud of our "Gu-Master" and team :)

# re: ASP.NET MVC 3: Layouts with Razor

Monday, October 25, 2010 3:11 AM by ScottGu

@Jesse,

>>>>>>> For legacy sites with webforms and MVC mix, the current view engine allowed us to use the webforms masterpage as the master page for our MVC views. Is there any chance razor will support something similar and work with

Page 15: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 15/21

existing master pages from the MVC/Webforms engine?

You can't share master pages between .aspx and razor views I'm afraid. However, you can have both in the sameapplication. You can also use the Html.RenderAction() and Html.RenderPartial() to have Razor based views call .aspxbased views and vice-versa.

Hope this helps,

Scott

# re: ASP.NET MVC 3: Layouts with Razor

Monday, October 25, 2010 3:12 AM by ScottGu

@christoph,

>>>>>> Scott, do you think there's a lot of changes coming until RTM or is it mostly polishing?! The roadmap at codeplexsuggests that there are only minor issues left. In any case, great job!

It is mostly polishing - with a few more features coming. Probably the biggest that people are looking for is intellisense withrazor based views inside VS 2010. That will show up with the next public preview.

Hope this helps,

Scott

# re: ASP.NET MVC 3: Layouts with Razor

Monday, October 25, 2010 3:13 AM by ScottGu

@allentranks,

>>>>>>> Great! Is there any difference between this and MasterPage? I mean, are there any features that Layout has whileMasterPage has not?

This is conceptually the same as a master-page. The core concepts are the same, with some differences in how they wereimplemented. One nice feature that razor has (which I'll cover in my next post) is a really easy way to create re-usablehelpers using a new @helper keyword. I'll cover that soon.

Hope this helps,

Scott

# re: ASP.NET MVC 3: Layouts with Razor

Monday, October 25, 2010 3:46 AM by Marius Schulz

Scott, thanks for this post! I like the Razor view engine more with every article I read about it ...

# re: ASP.NET MVC 3: Layouts with Razor

Monday, October 25, 2010 10:23 AM by Nick Masao

Thanks Scott, this is great. Razor is so clean.

# re: ASP.NET MVC 3: Layouts with Razor

Monday, October 25, 2010 3:58 PM by Phil Strong

Keep in mind that when you enable Razor you must update both your /Views/Web.config and your/Areas/[Area]/Views/Web.config

I was getting a lot of guff about @model not being in context

# re: ASP.NET MVC 3: Layouts with Razor

Page 16: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 16/21

Tuesday, October 26, 2010 3:16 AM by Asp.Net

Now with Razor it will be easy to work.

# re: ASP.NET MVC 3: Layouts with Razor

Wednesday, October 27, 2010 8:34 AM by [email protected]

The cleanest way would be to use .html and recognise that it is a razor page just by the content or a first line directive@language=vb | @language=cs

I don't know if that is allowed by the W3C, but come on, it is Microsoft.

# re: ASP.NET MVC 3: Layouts with Razor

Saturday, October 30, 2010 2:12 PM by microsoft office 2010

Will we be able to return multiple views in one call in mvc3 final or will that feature be pushed out for mvc4?

Thanks

# re: ASP.NET MVC 3: Layouts with Razor

Monday, November 01, 2010 8:14 AM by Prakash

Off topic, Scott Gu. Is Silverlight for web really dead? Are you going to use it just for Windows Phone 7?

# re: ASP.NET MVC 3: Layouts with Razor

Monday, November 01, 2010 11:07 AM by gmcoates

Looks good getting started with a new project using the Beta, really looking forward to the rtm.

# re: ASP.NET MVC 3: Layouts with Razor

Tuesday, November 02, 2010 6:29 AM by mogadanez

_ViewStart: why not use Controller name as layout name by default, and have some default layout name ( e.q. Application inSparkViewEngine ) that can be overriden( but not required ) in _ViewStart.

For me SparkViewEngine is more solid for now.

# re: ASP.NET MVC 3: Layouts with Razor

Tuesday, November 02, 2010 12:02 PM by Phil Strong

issue using Razor and MSPublish. MSPublish doesn't know to copy *.cshtml as output ... will this be fixed? Ideas on tempsolution?

# re: ASP.NET MVC 3: Layouts with Razor

Tuesday, November 02, 2010 2:39 PM by Naveen

I am planning to use razor engine for a small website project. It would be fun to learn and code. My only concern is whether Iwill be able to host my website with normal windows hosting providers ?

# re: ASP.NET MVC 3: Layouts with Razor

Tuesday, November 02, 2010 3:48 PM by Paul

@carlosmartinezt,

>>> The cleanest way would be to use .html and recognise that it is a razor page just by the content or a first line directive@language=vb | @language=cs <<<

Page 17: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 17/21

Err, that would mean that the web server would have to inspect EVERY file to determine whether to send it through the viewengine or not. HTML files are just "dumped" down the pipe, which is far more efficient. No, it is much better to change theextension so that the proper server extension can be called based on file name rather than content.

# re: ASP.NET MVC 3: Layouts with Razor

Tuesday, November 02, 2010 4:23 PM by Paul

+1 for the concept of an @layout directive instead of the code block. Layouts / Master Pages are SO common that thisdeserves an @ tag of its own. More, I would like for the directive to use the default search path (i.e., current view folder ->shared view folder for current area -> shared view folder for default area) so that you don't have to explicitly specify the fullpath of the view.

+1 also for disliking the _ViewStart page. Especially if it breaks partial views w/ AJAX. How about an @layout tag that cansimply point to a cshtml page that itself determines the layout.

MYPAGE.CSHTML

-------------

@model ...

@layout "mylayout.cshtml"

...

MYLAYOUT.CSHTML

---------------

@{

if(WHATEVER) {

Layout = "normal.cshtml";

} else {

Layout = "mobile.cshtml";

}

}

Not sure how you're supporting nested layouts and if this would break things, but it seems like this may be a much cleaner(and more flexible) way of doing things. A global start page just seems kludgy and prone to issues.

If you don't like having the @layout directive potentially call a page that CHANGES the layout, then perhaps an explicit@viewStart directive that calls a specific page rather than the specially named one.

Minimally, please provide some way of having a page that does NOT first call the _ViewStart page (like an @noViewStartdirective or something).

# re: ASP.NET MVC 3: Layouts with Razor

Tuesday, November 02, 2010 4:44 PM by LeeVi

Is Bob Muglia an idiot. I think the guy is a few screws loose !

Silverlight had such a bright future until that moron cast a shadow of doubt on the technology. Fortunately for me, I was juststarting to invest in Silverlight, and I had some large banking projects lined up with the technology.

You can appreciate that recommending Silverlight to a client now would be like shooting myself in the foot. You mean thattechnology that Microsoft relegated to the soon to be failure WP7.

# re: ASP.NET MVC 3: Layouts with Razor

Page 18: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 18/21

Wednesday, November 03, 2010 5:28 PM by Ras Fred

Do you know how I could find out the status of CAT.NET? It looks like a great enterprise tool for VS, but the beta was pulled& the CAT.NET team seems to have dropped off the map. Thanks much!

# re: ASP.NET MVC 3: Layouts with Razor

Friday, November 05, 2010 3:38 PM by MojoDK

@MartinF...

+1

I would like to know the same - how can we use _ViewStart in Areas? When I try, I get this error:

Unable to cast object of type 'ASP._ViewStart_vbhtml' to type 'System.Web.WebPages.StartPage'.

# re: ASP.NET MVC 3: Layouts with Razor

Monday, November 08, 2010 11:18 AM by Levi Bailey

Ok, I have found a couple more bugs with the beta. One is related to views, not layouts in particular, but them too. Whenyou add a new view to your project, the default build action for that view is none. The default should be content, or you getinto some real problems when you do a publish of the project from within Visual Studio. The second bug is morecomplicated and has nothing to do with views or layouts. So say I have an action defined as follows:

[HttpPost]

public ActionResult Save(ProductViewModel model)

{

...

return Json(model);

}

The ProductViewModel class is defined as follows:

public class ProductViewModel

{

public string Make { get; set; }

public string Model { get; set; }

...

}

Then on the view we call the save action from JQuery sending a Json Object as follows:

function Save(make, model)

{

var product = {

Make: make,

Model: model

};

var encoded = JSON.stringify(product);

$.ajax({

Page 19: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 19/21

url: '/Controller/Save',

type: "POST",

data: encoded,

dataType: "json",

contentType: 'application/json; charset=utf-8',

success: function(result) {

alert(JSON.stringify(result));

},

error: function() {

alert("Error occured.");

}

});

}

Then the following occurs. The Action Save receives a null view model. The key to this problem is that the variable namethat is passed to the action is one of the properties of the view model. If you change the Action method to anything else(except make) it will work. So if we change it as follows, everything will work as expected:

[HttpPost]

public ActionResult Save(ProductViewModel product)

{

...

return Json(product);

}

# re: ASP.NET MVC 3: Layouts with Razor

Tuesday, November 09, 2010 10:17 AM by ChrisGlenn

'Html' is ambiguous, imported from the namespaces or types 'System.Web.WebPages, System.Web.Mvc'.

I have been searching like crazy to find an answer to this error.

I am in VS 2010 MVC 3 empty project. I have added my first Controller and View. When I load it I get thise error.

Please advise on where I can find the answer.

# re: ASP.NET MVC 3: Layouts with Razor

Wednesday, November 10, 2010 3:12 AM by flash

In cshtml file:

帳號:@Context.User.Identity.Name

Razor engine show "error CS1002: it must be ;"

But "帳號" text is Multilingual characters.

How to write Multilingual Character in cshtml file?

# re: ASP.NET MVC 3: Layouts with Razor

Page 20: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 20/21

Wednesday, November 10, 2010 4:40 PM by davidebb

@flash: I pasted that line in a Razor file and did not get an error. Are you running the latest build? If so, can you please starta thread on the forum so we can discuss more easily? Thanks!

# re: ASP.NET MVC 3: Layouts with Razor

Tuesday, November 16, 2010 12:14 AM by ScottGu

@Levi,

>>>>>>> Ok, I have found a couple more bugs with the beta. One is related to views, not layouts in particular, but them too. When you add a new view to your project, the default build action for that view is none. The default should be content, or youget into some real problems when you do a publish of the project from within Visual Studio.

This first issue is because there wasn't Razor tooling in the beta. With the new RC build (and with the final release) thebuild action will by default be set to content. For views created with the beta you need to go back and change the contenttype explicitly to content.

The second issue is expected behavior. The name of the parameter affects model binding, so when the parameter isnamed “model” and the binding data contains “model”, we will bind that value to the object. This is how we’re able to bindmultiple model values to multiple parameters (i.e., you could create parameters “string make” and “string model” to extractthe values as well as the complex model).

You could also craft the JSON such that it matched the parameter name, if you really wanted to use “model” as theparameter name:

{

Model: {

Make: make,

Model: model

}

}

And then the data would have the equivalent names of “Model.Make” and “Model.Model”. Of course, with such binding, youwould need to name your parameter model now, as there’d be no other way to match the binding data coming in for thepost.

Hope this helps,

Scott

# re: ASP.NET MVC 3: Layouts with Razor

Friday, November 19, 2010 12:31 PM by Developer Seer

Hi Scott.

I'm wondering whats the best practice to create a logic for selecting phones views.

I Know Nerdinner has a way for this, and Scott Hanselman blogged about a new way using a custom ViewEngine to do this.

Now you blogged a third way for this, so When to use each one? is one better than the other ways?

Thanks in Advance.

# re: ASP.NET MVC 3: Layouts with Razor

Friday, December 03, 2010 12:33 PM by southwo8

Does Razor work in medium trust / partial trust environments?

Page 21: Layouts With Razor MVC

10/16/13 ASP.NET MVC 3: Layouts with Razor - ScottGu's Blog

weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx 21/21

# re: ASP.NET MVC 3: Layouts with Razor

Friday, December 10, 2010 7:40 AM by mrrgd

I am really excited about the layouts. I can see creating a sort of layout dictionary that defines the main and sub pagelayouts. I am correct that it should be possible to define regions within the index and assign different layouts to eachregion? And also the layouts themselves can contain nested layouts? This is kind of a decorator pattern for layouts. Nice....

# re: ASP.NET MVC 3: Layouts with Razor

Monday, December 13, 2010 5:58 AM by Pritam Baldota

Awesum blog. I liked to read most of the blog entries :)

# re: ASP.NET MVC 3: Layouts with Razor

Wednesday, December 15, 2010 7:41 AM by nick_uk

It looks like if you specify a ViewStart in the main view/shared i.e root of the web app, then this is not applied in areas.

I think some feature that applies common "ViewStart" logic, would be very useful - if I haven't missed a trick that is.

Otherwise, gotta say I am loving all the recent MVC 3, Razor and EF features. Take my hat off to Microsoft.

# re: ASP.NET MVC 3: Layouts with Razor

Monday, December 20, 2010 1:29 AM by vietgeek

When I render partial views via @Html.RenderAction(..) (Action was marked with ChildActionOnly) but the child view outputwas added the code from _ViewStart. I though _ViewStart code only call only one per page request, but it was called everyrender child action.

Terms of Use


Recommended