Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Arne 15 posts 36 karma points
    Jun 21, 2012 @ 09:42
    Arne
    1

    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>

    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.
    Thanks for your time!
    Arne

  • Stephen 767 posts 2268 karma points c-trib
    Jun 21, 2012 @ 09:59
    Stephen
    0

    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

  • Arne 15 posts 36 karma points
    Jun 21, 2012 @ 10:13
    Arne
    0

    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!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies