MVC HTML.RenderAction – Error: Duration must be a positive number

后端 未结 4 1707
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 04:27

On my website I want the user to have the ability to login/logout from any page. When the user select login button a modal dialog will be present to the user for him to enter i

相关标签:
4条回答
  • 2021-02-13 05:05

    Here are the fixes for this issue. http://thenullreference.com/blog/fixing-the-asp-net-mvc-3-outputcacheattribute-for-partial-views-to-honor-some-web-config-settings/

    0 讨论(0)
  • 2021-02-13 05:10

    Adding my comment as answer:

    Hmm, that's weird. By reading your question I am ready to bet 5 bucks that you have caching enabled somehow. Try renaming the action, controller. Make sure that the action you are calling is not decorated with the [OutputCache] attribute. Try doing this in a new project that you start from scratch. I am sure you will be able to narrow it down.

    0 讨论(0)
  • 2021-02-13 05:16

    I got around the problem by creating a custom OutputCache attribute, that manually loads the Duration, VarByCustom and VarByParam from the profile:

    public class ChildActionOutputCacheAttribute : OutputCacheAttribute
    {
        public ChildActionOutputCacheAttribute(string cacheProfile)
        {
            var settings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection("system.web/caching/outputCacheSettings");
            var profile = settings.OutputCacheProfiles[cacheProfile];
            Duration = profile.Duration;
            VaryByParam = profile.VaryByParam;
            VaryByCustom = profile.VaryByCustom;
        }
    }
    

    The advantage of this approach is that you get to still keep all your profiles in just one place in the web.config.

    This is also posted in the related question: https://stackoverflow.com/a/13866280/1373170

    0 讨论(0)
  • 2021-02-13 05:17

    In some cases it may be appropriate to just create a second action method, with caching disabled that is called by your primary action.

        /// Use this for normal HTTP requests which need to be cached
        [OutputCache(CacheProfile = "Script")]
        public ContentResult Foo(string id)
        {
            return _Foo(id);
        }
    
        /// Use this for Html.Action
        public ContentResult _Foo(string id)
        {
            return View();
        }
    

    When you need Html.Action you just call _Foo instead of Foo.

    @Html.Action("_Foo", "Bar").ToString();
    

    You can then rely on the parent page to do the caching.


    Another way is to just bypass the whole 'CacheProfile' thing for ActionMethod and use my 'DonutCacheAttribute' instead.

    The 'CacheProfile' for ActionMethods currently only respects the Duration and varyByParam properties - and this method makes it easy to set different caching durations in debug vs. deployment (assuming you're using XDT transformations).

    0 讨论(0)
提交回复
热议问题