Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can Razor not see ViewBag, Url.Content, etc. when the layout page is outside ~/Views/Shared?

I'm working on a CSS framework that I can drop in to other projects as a NuGet package.

To keep things clean, the entire framework - views, styles, images, and master pages / layout pages - is actually stored under ~/CssThing/

Works perfectly with the WebForms view engine, but when I move the _layout.cshtml file into ~/CssThing/ and then modify my Razor page to say:

@{
Layout = "~/CssThing/_layout.cshtml";
}

it starts complaining that ViewBag is not defined, or that The name 'Url' does not exist in the current context, or various other bits of weirdness that suggest the view is no longer inheriting from the proper base class.

How can I get this to work?

NOTE: The reason everything's split out like this is there's no way to force NuGet to overwrite existing code, and there's no way to spin up an empty MVC3 web application without picking up all the jQuery, etc. references, so rather than risk my framework getting half-deployed because half the files were already present, I'm keeping everything completely seperate.

like image 756
Dylan Beattie Avatar asked May 16 '11 21:05

Dylan Beattie


People also ask

How do I pass ViewBag from controller view?

To pass the strongly typed data from Controller to View using ViewBag, we have to make a model class then populate its properties with some data and then pass that data to ViewBag with the help of a property. And then in the View, we can access the data of model class by using ViewBag with the pre-defined property.

What is razor view in MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

What does return View() in MVC do?

The default behavior of the View method ( return View(); ) is to return a view with the same name as the action method from which it's called. For example, the About ActionResult method name of the controller is used to search for a view file named About.

How does ViewData work?

It is a dictionary type that stores the data internally. ViewData contains key-value pairs which means each key must be a string in a dictionary. The only limitation of ViewData is, it can transfer data from controller to view. It can not transfer in any other way and it is valid only during the current request.


2 Answers

Check you have a web.config in the new views directory. It's best to copy the one created by the default project in Views. This web.config defines Razor's settings like default page base class and what namespaces are implicitly available. Those namespaces define what HTML helpers you can use.

like image 160
Andrew Davey Avatar answered Oct 20 '22 02:10

Andrew Davey


You need to manually specify @inherits WebViewPage, or add the default Web.config section (which sets the base type) to the folder with the views.

like image 30
SLaks Avatar answered Oct 20 '22 01:10

SLaks