如何确保HTTPContext有价值?

时间:2009-10-01 16:31:49

标签: asp.net-mvc extension-methods httpcontext

我正在构建MVC店面版本。我有两个主题,一个是标准的ui,另一个是admin ui。我有一些HTML Helper方法,它们将.ascx文件提供给.aspx文件,就像示例一样,但是没有插件。所有的东西都适用于默认主题,但它们都不适用于管理主题。当Html.SimpleActivityTypeList()触发时,我总是得到相同的错误。 Html.RenderPartial(“AdminLeftNav”)将始终正常工作。

<asp:Content ID="Content3" ContentPlaceHolderID="NavContent" runat="server">
    <% Html.SimpleActivityTypeList(); %>
    <% Html.RenderPartial("AdminLeftNav"); %>
</asp:Content>

我在扩展类中添加了如下方法。

public static void SimpleActivityTypeList(this HtmlHelper helper)
    {
        helper.RenderAction<CatalogController>(x => x.SimpleActivityTypeList());
     }

正如我所说,这在默认主题中工作正常。 请帮帮忙?

Stack Trace: 


[ArgumentNullException: Value cannot be null.
Parameter name: httpContext]
   System.Web.Routing.RequestContext..ctor(HttpContextBase httpContext, RouteData routeData) +85
   Microsoft.Web.Mvc.ViewExtensions.RenderRoute(HtmlHelper helper, RouteValueDictionary values) +367
   Microsoft.Web.Mvc.ViewExtensions.RenderAction(HtmlHelper helper, Expression`1 action) +113
   GrandGuide.Web.CMSExtensions.SimpleActivityTypeList(HtmlHelper helper) in C:\S3_Depot\S3_Dev\GrandGuide\GrandGuide.Web\Infrastructure\Helpers\CMSExtensions.cs:55
   ASP.views_themes_admin_index_aspx.__RenderContent3(HtmlTextWriter __w, Control parameterContainer) in c:\S3_Depot\S3_Dev\GrandGuide\GrandGuide.Web\Views\Themes\Admin\Index.aspx:7
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   ASP.views_themes_admin_theme_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\S3_Depot\S3_Dev\GrandGuide\GrandGuide.Web\Views\Themes\Admin\Theme.Master:33
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +71
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

1 个答案:

答案 0 :(得分:0)

更多信息会有所帮助。在过去,我使用过:

if (null != HttpContext)

或者,如果您在上下文中访问某些内容,例如会话变量:

if (null != HttpContext.Current.Session["my_var"])