ViewMasterPage - what is MVC 3's Razor Equivalent? - asp.net-mvc-3

I'm converting MVC 2 over to MVC 3, and I have a custom class that inherits from System.Web.Mvc.ViewMasterPage for my Default.Master file.
When converting over to MVC 3, I still want to have a custom class for my Layout.cshtml file, but what should I inherit from? I'm looking for something like System.Web.Mvc.ViewLayoutPage and System.Web.Mvc.ViewLayoutPage<>, but they do not exist.
Thanks!

There is no equivalent. Master pages in Razor are simple .cshtml files. In fact there is no distinction between a view and a layout. Both are simple .cshtml templates. It's only the ~/Views/_ViewStart.cshtml file which specifies which layout to be used by default. Obviously you could always create a custom layout template in the ~/Views/Shared folder and then specify it either in the ~/Views/_ViewStart.cshtml or override it in each specific view or also in the controller action when returning a view you have the possibility of specifying a master page.
For more information about Razor views you may checkout the following blog post.

Related

Adding a view to MVC that doesn't belong to a controller

I'm not sure if the question title really explains what I want to do, however I will explain below:
I am using the Visual Studio MVC project template and I have changed some of the tabs to map to different actions from different controllers. However I want to make one of the tabs to open a view that will again have links for different administration actions.
The problem I have is that I am unsure where to place this view as it doesn't really belong to an admin controller as each tab on this view will link to a list view in another controller. In effect it is a sub _Layout view, as it doesn't have anything to do with a controller.
I hope I have made myself clear enough!
You can place this view in the Shared folder since it will be used by multiple controllers. Or, you could place it somewhere else and reference it by using the full path to this View/Partial View
In a Controller
public ActionResult SomeAction(){
return View("~/Path/To/View/ViewName.cshtml");
}
In a View (Razor)
#Html.RenderPartial("~/Path/To/View/ViewName.cshtml");
With that said, the Shared folder makes the most sense since it will be shared across multiple controllers.

Web page (like webmatrix) in mvc3 app

Could i include a single page .cshtml in a MVC3 app (without controller)? I have some static pages but i like to use our base layout.
Yes, you can. For example include index.cshtml with the following contents in the root of your web site:
#DateTime.Now
and then navigate to /index.cshtml.
Bare in mind that cshtml pages are not allowed in the ~/Views folder so make sure you don't put outside. The Views folder is a special one and is controlled by the ~/Views/web.config in which the base type for Razor views is changed to System.Web.Mvc.WebViewPage because those are MVC views and also they cannot be served directly.
So you could have 2 types of templates:
System.Web.WebPages.WebPage (standard WebMatrix WebPage)
System.Web.Mvc.WebViewPage (ASP.NET MVC views, stored in the ~/Views folder)
This being said, you cannot use your ~/Views/Shared/_Layout.cshtml with a WebPage. It can only be used with ASP.NET MVC views.

Using #helpers from another View in Razor ASP.Net MVC3

I want to write a few simple #helpers to use in several views.
I want them to be inside a Razor .cshtml file (not in a c# class) to have the HTML syntax highlighted.
I can easily access #helpers written within the same View, I can separate them into Helpers.cshtml, and if I put this Helpers.cshtml into an App_Code folder I can access it from any View via #Helpers.MyHelper(). But I want them to be accessible only for a few pages.
I think, it could be like putting a #using if the helpers are in c# class, but what is the namespace for just another view?..
Sorry, but that's not possible with helpers stored in the App_Code folder as .cshtml files. They will be shared among all views.

mvc 3 razor view engine using "default" views

i'm using Razor for a new project in my company, and i have been playing with it for 2 days and already found some weird behaviour imho.
I have a home controller in the root of my webapp and a home controller in an area , call it Area1. In both these controllers I have an Index action and therefore I got an Index view in the Views root folder, and another one in the Area1\Views folder. If I remove the index view within the area , so Area1\Views\Index.cshtml and I request Area1\Home\Index, I don't get an error about the view engine not finding the view for that action , but the "base" Index view is found in \Views\Index.cshtml and rendered.
Someone knows if this is a bug or it's done by purpose ? If so, there's any way to disable this default ?
Definitely NOT a bug.
This behaviour allows easy reuse of View templates, eg, for error handling.
The default behaviour for ASP.NET MVC is that if you do...:
return View();
...it searches for the view template with the name of the action to use in a number of places: the default naming convention folder for the controller, ie, /Area1/Views/Home/, then /Area1/Views/Shared/ then Area1/Views/ then /Views/Shared/ then /Views/
If it finds no such View matching the name of the action, then it throws an error.
So much for the default behaviour. To "customize" this behaviour, you just need to do the following:
In your controller actions, you can specify the name of the View template to use when you return. EG:
return View("MyOtherView");
or better still, if using T4MVC:
return View(MVC.Area1.Home.Views.MyOtherView); // does away with "magic" strings
As a result, I don't see that you need to switch off the default behaviour to be able to do (whatever) you want. Controllers are there to, uhmmmm, control what views are used to display to the user. That is the best practice.
However, ASP.NET MVC is very configurable, so there are ways and means, I presume, to switch this off.
If you want to do this, good luck to you, but it makes much more sense to follow the defaults and get to understand how ASP.NET MVC works, especially if you are a beginner.
NOTE:
The above applies to ASP.NET MVC 1, 2, 3 and will continue to do so. It is the default behaviour for all View engines, including Razor and WebForm Views.
PS:
And you can configure the urls using route registration if your concern is the look of the url in the user's browser.

How to specify default LayoutPage in Razor in ASP.NET MVC 3 Preview 1?

I want to specify (in one place) a default layout page in Razor, so that I can delete this:
#{ LayoutPage = "~/Views/Shared/_Layout.cshtml"; }
from every .cshtml file I have. But I don't know how... Any ideas? I'm using Razor engine from ASP.NET MVC 3 Preview 1.
Create a "~/Views/_ViewStart.cshtml" page and the following inside:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Note that you can write code in here, so it is possible to change your layout based on the type of device targeted, etc.
This is now created by default in an empty MVC3 project.
Source
It looks like the way to do this is by using a _init.cshtml file in the root of the view directory in which you would like a common page element (header). When the Razor view engine builds your page it looks for a few specific files automatically called _start.cshtml, _init.cshtml, and _end.cshtml; these files are loaded in respective order by the view engine for every request. Placing the LayoutPage definition, and/or other common initialization operations in these files will ensure they're run for all pages.
Note: I'm not sure if the effect is passed down into sub-directories as it wasn't clear from the documentation; you'll have to give it a try and find out.
There's quite a bit more detailed information on how to do this found in the Microsoft how-to book on building pages with Razor. I found the section Running Code Before and After Files in a Folder on page 169. Check this Microsoft download page for the full book as well as additional Razor samples.
There is no easy way to do this in MVC 3 Preview 1. This is a limitation of the preview bits that will be addressed in upcoming releases. Unfortunately _init.cshtml files do not work in this preview of MVC3 so you cannot follow the Web Pages pattern.
There are 2 ways I can think of to make it work (though neither is optimal)
write your own page base class that derives from WebViewPage and sets the right Layout in the constructor... but in that case you would have to specify an #inherits directive in every view.
set the layout override in your action method (using the View(string viewName, string masterName) override). You could write an intermediate controller base class that would have a helper method to save yourself the trouble of repeating the layout everywhere.

Resources