Copied to clipboard

Flag this post as spam?

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


  • Barry Fogarty 493 posts 1129 karma points
    Feb 04, 2015 @ 15:46
    Barry Fogarty
    0

    Donut caching error when posting to an action

    Just wondering if you have come accross the following issue when using a Donut caching with a http POST to a controller action:

    NullReferenceException: Object reference not set to an instance of an object.]
       
    DevTrends.MvcDonutCaching.KeyGenerator.GenerateKey

    I have a cache set on my route-hijacked surface controller for the page, e.g.

    [DonutOutputCache(CacheProfile = "OneDay")]

    and I also have a cache 'hole' defined in my action like so

    @Html.Action("RenderRegistrationForm","Registration", true)

    The above error occurs when I post to my controller action to handle the form, e.g.

    Html.BeginUmbracoForm<RegistrationController>("HandleRegisterMember")

    If I remove the cache attribute on the parent surface controller, it works as expected (but of course then nothing gets cached).  Is this a bug with donutcaching or am I implementing it wrong?  I would have thought any kind of post simply ignores cache altogether.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 04, 2015 @ 16:44
    Jeroen Breuer
    0

    Hello,

    I'm not sure if you are implementing it correct or wrong. Why do you want to cache an ActionResult which you post to? If you cache that the model which gets posted is cached and I don't think you want that. 

    Jeroen

  • Barry Fogarty 493 posts 1129 karma points
    Feb 04, 2015 @ 18:02
    Barry Fogarty
    0

    Hi Jeroen - Thats the thing - I dont want to cache it.  I want to cache the rest of the page, but I get the above error if I have Donutcaching enabled on the parent surface controller (i.e. the route-hijacked surface controller associated with the doctype).  However, it only happens on post to a child action.

    Cheers

    Barry

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 04, 2015 @ 18:11
    Jeroen Breuer
    0

    Could you perhaps show the entire controller? It's hard to find out what's wrong now.

    Jeroen

  • Barry Fogarty 493 posts 1129 karma points
    Feb 04, 2015 @ 18:23
    Barry Fogarty
    0

    Sure, its very simple.  Parent Surface Controller:

    public class MemberRegistrationController : SurfaceRenderMvcController
        {
            [DonutOutputCache(CacheProfile = "OneDay")]
            public ActionResult Index()
            {
                var model = ModelLogic.CreateMasterModel() as MasterModel<Models.Generated.MemberRegistration>;
     
                return CurrentTemplate(model);
            }

            

        }

    Child action:

    [HttpPost]
            public ActionResult HandleRegisterMember(CustomerModel model)
            {
                if (ModelState.IsValid == false)
                {
                    return CurrentUmbracoPage();
                }
     
                try
                {
                    var memberService = Services.MemberService;
                    if (memberService.GetByEmail(model.Email) != null)
                    {
                        ModelState.AddModelError("DuplicateEmail", "A Member with that email already exists.  Did you mean to <a href='/login'>Login</a>?");
                        return CurrentUmbracoPage();
                    }
     
                    var member = RegisterMember(model);
     
                    // Log them in
                    FormsAuthentication.SetAuthCookie(member.Username, model.CreatePersistentLoginCookie);
     
                    TempData["FormSuccess"] = true;
     
                }
                catch (Exception ex)
                {
                    // EG: Duplicate email address - already exists
                    ModelState.AddModelError("MemberCreate", ex.Message);
                }
     
                // TODO: Redirect to success (dashboard? cart?) page
                return CurrentUmbracoPage();
            }

     

    It doesnt matter what is in the POST action, even just doing a return CurrentUmbracoPage() throws the error.

    As I said removing the DonutOutputCache attribute on the parent controller fixes the issue.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 04, 2015 @ 18:26
    Jeroen Breuer
    0

    Hmm if you look at the contact controller from the best practises you can see something which is exactly the same: https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/Controllers/ContactController.cs

    That just works so I don't why you are getting this error now.

    Jeroen

  • Barry Fogarty 493 posts 1129 karma points
    Feb 04, 2015 @ 18:49
    Barry Fogarty
    0

    The difference is I am rendering my form via an action method on my template:

    @Html.Action("RenderRegistrationForm","Registration",true)

    Sorry, I should have posted the code for that action method before.  Again its very simple, creates a model and returns a partial view.

    [ChildActionOnly]
            public ActionResult RenderRegistrationForm()
            {
                //Create a new Login View Model
                var customerModel = new CustomerModel();
     
                return PartialView("Member/Register", customerModel);
            }

     

    In the Hybrid Framework the contact form is embedded directly into the template, so it would never be able to be 'donut-holed' (I dont think?)  So, for example, if this was a login form there would be no way to exclude it from the cache while ensuring the rest of the page was cached.

     

     

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 04, 2015 @ 18:51
    Jeroen Breuer
    0

    Yes that is a difference. Why does you register form needs to be 'donut-holed'?

    Jeroen

  • Barry Fogarty 493 posts 1129 karma points
    Feb 04, 2015 @ 22:26
    Barry Fogarty
    1

    Let's say the user is already logged in, I might like to redirect to the 'member dashboard' action. 

    It would be the same deal for a login form where there might be lots of other content on the page that would be useful to have cached.  

    On his blog post he specifically talks about donut caching login forms so I don't think its a bug in the tool itself.  Could be something upstream in Umbraco but chances are I'm just not doing it right!  WIll test in pure MVC tomorrow and report.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Feb 10, 2015 @ 10:08
    Jeroen Breuer
    0

    Hello,

    Did you test it with a pure MVC and did that work?

    Jeroen

  • Barry Fogarty 493 posts 1129 karma points
    Feb 11, 2015 @ 16:21
    Barry Fogarty
    0

    Hi Jeroen,

    I'm on another project atm but I will get back on this later in the week and feedback any info, cheers.

  • Barry Fogarty 493 posts 1129 karma points
    Mar 10, 2015 @ 17:05
    Barry Fogarty
    102

    NB Jeroen I resolved this by updating to the latest beta of the DonutCache tool.

    https://www.nuget.org/packages/MvcDonutCaching/1.3.1-beta1

  • Simon 692 posts 1068 karma points
    Nov 29, 2016 @ 17:18
    Simon
    0

    Hi Barry,

    How can I update because it is not showing up on nuget the latest version and it is not allowing me to update it as well through nuget console. Not Found.

    Any idea?

    Kind Regards

  • Barry Fogarty 493 posts 1129 karma points
    Nov 30, 2016 @ 14:54
    Barry Fogarty
    0

    Simon - I just checked on Nuget and you'll probably need to use the latest version, which is installed by using

    Install-Package MvcDonutCaching -Pre
    

    Here is the Nuget page:

    https://www.nuget.org/packages/MvcDonutCaching/1.3.1-rc1

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 10, 2015 @ 17:11
    Jeroen Breuer
    0

    Good to hear you've got it fixed!

  • Rune Grønkjær 1372 posts 3103 karma points
    Nov 11, 2015 @ 08:07
    Rune Grønkjær
    0

    This worked for me as well. Thanks Bary. HFYR

    /Rune

Please Sign in or register to post replies

Write your reply to:

Draft