I've read a post about changing base view type on MVC from the link below:
http://haacked.com/archive/2011/02/21/changing-base-type-of-a-razor-view.aspx
I followed the instructions but my page still inherits from System.Web.Mvc.WebViewPage
. I can't reach any property defined in my custom view base and I get an error on runtime. When I use @inherits
keyword, it fixes.
Web.config
<pages pageBaseType="[MyNamespace].WebViewPageBase">
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
WebViewPageBase
public class WebViewPageBase : WebViewPage
{
public SomeType MyProperty { get; set; }
public override void InitHelpers()
{
base.InitHelpers();
MyProperty = { foo };
}
public override void Execute()
{
}
}
public class WebViewPageBase<T> : WebViewPage<T>
{
public SomeType MyProperty { get; set; }
public override void InitHelpers()
{
base.InitHelpers();
MyProperty = { foo };
}
public override void Execute()
{
}
}
Partial View
@model TopMenuModel
<div class="topMenu">
@MyProperty
</div>
But in the post I've read there is no instruction about @inherits
keyword. Is there any thing that I miss or any way to make this work without @inherits
keyword in all pages?
SOLVED:
web.config
file in root
directory is not the right one. I changed base type in the web.config
file under View
directory and it fixed.
The problem you're having is because you need to modify the VIEWS folder's web.config file. So basically this line:
needs to be in the web.config from your views folder not the main project's web.config. Darin Dimitrov's answer clearly specifies that, but people usually overlook that detail. I know because I was one of them. Actually so does Phil Haack's article...
Why did you show two versions of
WebViewPageBase
: generic and non-generic?You only need the generic version:
and then:
Now inside your views you will be able to use the property:
UPDATE:
Step by step setup:
Add a custom base view:
Set the
pageBaseType
attribute in~/Views/web.config
(not to be confused with~/web.config
):Inside
~/Views/Home/Index.cshtml
use the property:Hit Ctrl+F5 to run the application and if everything goes well you will be greeted with a
Hello World
.