asp.net-mvc MVC HTML.RenderAction – 错误:持续时间必须是正数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4997989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:53:15  来源:igfitidea点击:

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

asp.net-mvcasp.net-mvc-3razor

提问by Mike Barlow - BarDev

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 in his credentials.

在我的网站上,我希望用户能够从任何页面登录/注销。当用户选择登录按钮时,将向用户显示一个模式对话框,供他输入其凭据。

Since login will be on every page, I thought I would create a partial view for the login and add it to the layout page. But when I did this I got the following error: Exception Details: System.InvalidOperationException: Duration must be a positive number.

由于登录将在每个页面上,我想我会为登录创建一个局部视图并将其添加到布局页面。但是当我这样做时,我得到了以下错误: 异常详细信息:System.InvalidOperationException:持续时间必须是一个正数。

There are other ways to work around this that would not using partial views, but I believe this should work.

还有其他不使用局部视图的方法可以解决这个问题,但我相信这应该可行。

So to test this, I decided to make everything simple with the following code:

因此,为了测试这一点,我决定使用以下代码使一切变得简单:

Created a layout page with the following code

使用以下代码创建了一个布局页面

@{Html.RenderAction("_Login", "Account");}

In the AccountController:

在 AccountController 中:

public ActionResult _Login()
{
    return PartialView("_Login");
}

Partial View _Login

部分查看_登录

<a id="signin">Login</a>

But when I run this simple version this I still get this error: Exception Details: System.InvalidOperationException: Duration must be a positive number.

但是当我运行这个简单版本时,我仍然收到这个错误: 异常详细信息:System.InvalidOperationException:Duration must be a positive number。

Source of error points to "@{Html.RenderAction("_Login", "Account");}"

错误来源指向"@{Html.RenderAction("_Login", "Account");}"

There are some conversations on the web that are similar to my problem, which identifies this as bug with MVC (see links below). But the links pertain to Caching, and I'm not doing any caching.

网络上有一些与我的问题类似的对话,将其标识为 MVC 的错误(请参阅下面的链接)。但是链接与缓存有关,我没有做任何缓存。

OuputCache Cache Profile does not work for child actions
http://aspnet.codeplex.com/workitem/7923

OuputCache 缓存配置文件不适用于子操作
http://aspnet.codeplex.com/workitem/7923

Asp.Net MVC 3 Partial Page Output Caching Not Honoring Config Settings Asp.Net MVC 3 Partial Page Output Caching Not Honoring Config Settings

Asp.Net MVC 3 部分页面输出缓存不尊重配置设置 Asp.Net MVC 3 部分页面输出缓存不尊重配置设置

Caching ChildActions using cache profiles won't work? Caching ChildActions using cache profiles won't work?

使用缓存配置文件缓存 ChildActions 不起作用? 使用缓存配置文件缓存 ChildActions 不起作用?

I'm not sure if this makes a difference, but I'll go ahead and add it here. I'm using MVC 3 with Razor.

我不确定这是否有影响,但我会继续在这里添加它。我将 MVC 3 与 Razor 一起使用。



Update
Stack Trace

更新
堆栈跟踪

[InvalidOperationException: Duration must be a positive number.]
   System.Web.Mvc.OutputCacheAttribute.ValidateChildActionConfiguration() +624394
   System.Web.Mvc.OutputCacheAttribute.OnActionExecuting(ActionExecutingContext filterContext) +127
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +72
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
   System.Web.Mvc.<>c__DisplayClass4.<Wrap>b__3() +15
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +41
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1363

[HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +2419
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +275
   System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +94
   System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +838
   System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) +56
   ASP._Page_Views_Shared_SiteLayout_cshtml.Execute() in c:\Projects\prj Projects\prj\Source\Presentation\prj.PublicWebSite\Views\Shared\SiteLayout.cshtml:80
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +173
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +234
   System.Web.WebPages.WebPageBase.PopContext() +234
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +384
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +33
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +784900
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +784900
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +265
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371


Update
When I Break in Code, it errors at @{Html.RenderAction("_Login", "Account");} with the following exception. The inner exception

更新
当我中断代码时,它在 @{Html.RenderAction("_Login", "Account");} 处出错,但有以下异常。内部异常

Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.

at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride)
   at System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage)
   at System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm)
   at System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter)
   at System.Web.Mvc.Html.ChildActionExtensions.RenderAction(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues)
   at ASP._Page_Views_Shared_SiteLayout_cshtml.Execute() in c:\Projects\prj Projects\prj\Source\Presentation\prj.PublicWebSite\Views\Shared\SiteLayout.cshtml:line 80
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.WebPages.WebPageBase.Write(HelperResult result)
   at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
   at System.Web.WebPages.WebPageBase.PopContext()
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)

enter image description here

在此处输入图片说明



Answer Thanks Darin Dimitrov

回答 感谢 Darin Dimitrov

Come to find out, my AccountController had the following attribute

来看看,我的 AccountController 有以下属性

[System.Web.Mvc.OutputCache(NoStore =true, Duration = 0, VaryByParam = "*")].

[System.Web.Mvc.OutputCache(NoStore =true, Duration = 0, VaryByParam = "*")]。

I don't believe this should caused a problem, but when I removed the attribute everything worked.

我不相信这会引起问题,但是当我删除该属性时,一切正常。

BarDev

巴德夫

回答by Darin Dimitrov

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.

嗯,这很奇怪。通过阅读您的问题,我准备赌 5 美元您以某种方式启用了缓存。尝试重命名操作,控制器。确保您正在调用的操作未使用该[OutputCache]属性进行修饰。尝试在从头开始的新项目中执行此操作。我相信你将能够缩小范围。

回答by Pablo Romeo

I got around the problem by creating a custom OutputCacheattribute, that manually loads the Duration, VarByCustomand VarByParamfrom the profile:

我通过创建一个自定义OutputCache属性来解决这个问题,该属性从配置文件中手动加载Duration,VarByCustomVarByParam

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.

这种方法的优点是您仍然可以将所有配置文件仅保存在 web.config 中的一个位置。

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

这也发布在相关问题中:https: //stackoverflow.com/a/13866280/1373170

回答by Simon_Weaver

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.Actionyou just call _Foo instead of Foo.

当您需要时,Html.Action您只需调用 _Foo 而不是 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.

另一种方法是为 ActionMethod 绕过整个 'CacheProfile' 内容,而使用我的 'DonutCacheAttribute'

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

ActionMethods 的 'CacheProfile' 当前仅考虑DurationvaryByParam属性 - 并且此方法可以轻松地在调试与部署中设置不同的缓存持续时间(假设您正在使用 XDT 转换)。