We're currently setting up a new Umbraco 4.7.2 project, of course using Razor. In MVC projects, I know it's possible to use a custom BaseView. You just create a new class like this:
using System; namespace BISBV.GUI { public class BaseView<TModel> : System.Web.Mvc.WebViewPage<TModel> { #region Custom
// Place some custom properties here, accessible by using @CustomProperty in your views...
#endregion
public BaseView() { }
public override void Execute() { base.ExecutePageHierarchy(); } } }
After creating and building, you've got to reference this file in your Web.config (the same config-section exists in an Umbraco installation, as you all know):
I know Umbraco 4.7.2 isn't build in MVC. However, the above piece of config stuff is in Umbraco's .config as well. When trying to get the same result in Umbraco, I create a class, inheriting from DynamicNodeContext like this:
public abstract class BaseDynamicNodeContext : DynamicNodeContext { public int SiteId { get { return Convert.ToInt32(Request.QueryString["siteid"]); } } }
After referencing this file in my Web.config as I explained above in the MVC example, I get strange System.AccessViolationException's, saying some memory might be corrupt. Is there anyone who knows how to solve this? I'd love to have my custom base view in Umbraco macro's, so I can some globally-used properties in there.
Thank you Stephen, you put me on the right track. In a property called "SiteId", I tried to return a QueryString value by using Request.QueryString["siteid"]. Apparently, this was throwing the "Attempting to read or write protected memory"-exceptions.
I resolved it by using the HttpContext-class (in bold), instead of the Request-property directly:
Custom Razor baseview in Macro's?
Hello everyone,
We're currently setting up a new Umbraco 4.7.2 project, of course using Razor. In MVC projects, I know it's possible to use a custom BaseView. You just create a new class like this:
using System;
namespace BISBV.GUI
{
public class BaseView<TModel> : System.Web.Mvc.WebViewPage<TModel>
{
#region Custom
// Place some custom properties here, accessible by using @CustomProperty in your views...
#endregion
public BaseView()
{
}
public override void Execute()
{
base.ExecutePageHierarchy();
}
}
}
After creating and building, you've got to reference this file in your Web.config (the same config-section exists in an Umbraco installation, as you all know):
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="BISBV.GUI.BaseView">
<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>
</system.web.webPages.razor>
{
public int SiteId
{
get
{
return Convert.ToInt32(Request.QueryString["siteid"]);
}
}
}
We run a very similar solution here and it works fine. We have *not* registered anything in the web.config. All our views begin with
@inherits OurOwnCustomNodeContext
@using OurOwnCode.Whatever
And our context is defined as OurOwnCode.Whatever.OurOwnCustomNodeContext : DynamicNodeContext
Do you have a stack trace for your exception?
Stephan
Thank you Stephen, you put me on the right track. In a property called "SiteId", I tried to return a QueryString value by using Request.QueryString["siteid"]. Apparently, this was throwing the "Attempting to read or write protected memory"-exceptions.
I resolved it by using the HttpContext-class (in bold), instead of the Request-property directly:
return Convert.ToInt32(HttpContext.Current.Request.QueryString["siteid"]);
Problem solved!
is working on a reply...