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.
Why did you show two versions of WebViewPageBase: generic and non-generic?
You only need the generic version:
public class MyWebView<T> : WebViewPage<T>
{
public SomeType MyProperty { get; set; }
public override void InitHelpers()
{
base.InitHelpers();
MyProperty = new SomeType();
}
public override void Execute()
{
}
}
and then:
<pages pageBaseType="MvcApplication1.WebViews.MyWebView">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
Now inside your views you will be able to use the property:
@model TopMenuModel
<div class="topMenu">
@MyProperty
</div>
UPDATE:
Step by step setup:
- Create a new ASP.NET MVC 3 application using the Internet Template
Add a custom base view:
namespace MvcApplication1 { public class MyWebView<T> : WebViewPage<T> { public string MyProperty { get; set; } public override void InitHelpers() { base.InitHelpers(); MyProperty = "Hello World"; } public override void Execute() { } } }Set the
pageBaseTypeattribute in~/Views/web.config(not to be confused with~/web.config):<pages pageBaseType="MvcApplication1.MyWebView"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> </namespaces> </pages>Inside
~/Views/Home/Index.cshtmluse the property:<div> @MyProperty </div>Hit Ctrl+F5 to run the application and if everything goes well you will be greeted with a
Hello World.
The problem you're having is because you need to modify the VIEWS folder's web.config file. So basically this line:
<pages pageBaseType="[MyNamespace].WebViewPageBase">
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...
来源:https://stackoverflow.com/questions/14358410/changing-base-view-type-on-mvc4