【问题标题】:Issue with Bundling and Release mode捆绑和发布模式的问题
【发布时间】:2014-01-31 23:14:47
【问题描述】:

每当我使用debug="false" 模式和BundleTable.EnableOptimizations = true 发布我的网站时 服务器为捆绑的脚本和样式返回以下异常,而请求仅针对非授权请求!但是,如果我使用debug="true" 运行应用程序或用户已登录(通过发布模式下的表单身份验证),则不会发生异常,并且每项操作都可以正常工作。

我认为捆绑和身份验证之间应该有关系!!如异常所示。

怎么了?

我的捆绑代码:

    public static void RegisterBundles(BundleCollection bundles)
    {
        StyleBundle bundleStyles = new StyleBundle("~/bundles/styles/");
        bundleStyles.Include(
            "~" + Paths.Scripts.AdminSkin.css.reset_css,
            "~" + Paths.Scripts.AdminSkin.css.common_css,
            "~" + Paths.Scripts.AdminSkin.css.form_css,

            //do not move this line to blow
            "~" + Paths.Scripts.site.css.standard_css,

            "~" + Paths.Scripts.AdminSkin.css.standard_css,
            "~" + Paths.Scripts.AdminSkin.css.special_pages_css,

            //"~" + Paths.Scripts.AdminSkin.css.simple_lists_css,
            //"~" + Paths.Scripts.AdminSkin.css.block_lists_css,
            //"~" + Paths.Scripts.AdminSkin.css.planning_css,
            //"~" + Paths.Scripts.AdminSkin.css.table_css,
            //"~" + Paths.Scripts.AdminSkin.css.calendars_css,
            //"~" + Paths.Scripts.AdminSkin.css.wizard_css,
            //"~" + Paths.Scripts.AdminSkin.css.gallery_css,

            "~" + Paths.Scripts.site.css.login_css,
            "~" + Paths.Scripts.site.css.site_css,
            "~" + Paths.Scripts.site.css.tables_css,
            "~" + Paths.Scripts.site.css.standard_fa_css,


            "~" + Paths.Scripts.site.css.theme_selector_css,
            "~" + Paths.Scripts.jquery_ui_1_9_2.css.redmond.jquery_ui_1_9_2_custom_css,
            "~" + Paths.Scripts.ImageGallery.wt_rotator_css,
            "~" + Paths.Scripts.ImageGallery.default_css,
            "~" + Paths.Scripts.calendar.aqua.theme_css
            );

        ScriptBundle bundleScripts = new ScriptBundle("~/bundles/scripts/");
        bundleScripts.Include(
            //Admin Skin Scripts
            "~" + Paths.Scripts.AdminSkin.js.old_browsers_js,
            "~" + Paths.Scripts.AdminSkin.js.libs.jquery_hashchange_js,
            "~" + Paths.Scripts.AdminSkin.js.jquery_accessibleList_js,
            "~" + Paths.Scripts.AdminSkin.js.searchField_js,
            "~" + Paths.Scripts.AdminSkin.js.common_js,
            "~" + Paths.Scripts.AdminSkin.js.standard_js,
            "~" + Paths.Scripts.AdminSkin.js.jquery_tip_js,
            "~" + Paths.Scripts.AdminSkin.js.jquery_contextMenu_js,
            "~" + Paths.Scripts.AdminSkin.js.jquery_modal_js,
            "~" + Paths.Scripts.AdminSkin.js.list_js,
            //"~" + Paths.Scripts.AdminSkin.js.libs.jquery_dataTables_min_js,

            //jquery plug ins
            "~" + Paths.Scripts.jquery_ui_1_9_2.js.jquery_ui_1_9_2_custom_js,
            "~" + Paths.Scripts.jquery_cookie.jquery_cookie_js,


            "~" + Paths.Scripts.site.js.site_js,
            "~" + Paths.Scripts.site.js.clock_js,
            "~" + Paths.Scripts.site.js.size_js,
            "~" + Paths.Scripts.site.js.cookie_manager_js,


            //devexpress related
            "~" + Paths.Scripts.site.js.devexpress_theme_js,
            "~" + Paths.Scripts.site.js.devexpress_controls_js,

            //chart scripts
            "~" + Paths.Scripts.Highstock_1_2_5.highstock_src_modified_js,
            "~" + Paths.Scripts.Highstock_1_2_5.exporting_js,
            "~" + Paths.Scripts.Highstock_1_2_5.highcharts_more_js,
            "~" + Paths.Scripts.site.js.chart_fa_js,

           //calendar scripts
            "~" + Paths.Scripts.calendar.jalali_js,
            "~" + Paths.Scripts.calendar.calendar_js,
            "~" + Paths.Scripts.calendar.calendar_setup_js,
            "~" + Paths.Scripts.calendar.calendar_fa_js,
            "~" + Paths.Scripts.calendar.calendar_en_js,

            //image gallery scripts
            "~" + Paths.Scripts.ImageGallery.js.jquery_wt_rotator_min_js,
            "~" + Paths.Scripts.ImageGallery.js.jquery_easing_1_3_min_js,
            "~" + Paths.Scripts.ImageGallery.js.preview_js
            );


        if (RequestCachedItems.Setting.UseGoogleMap)
        {

        }
        else
        {
            //leaflet map script
            bundleScripts.Include("~" + Paths.Scripts.leaflet_0_7.leaflet_src_js);
            //leaflet map style
            bundleStyles.Include("~" + Paths.Scripts.leaflet_0_7.leaflet_css, new CssRewriteUrlTransform());
        }

        ScriptBundle bundleLiveScripts = new ScriptBundle("~/bundles/livescripts/");
        bundleLiveScripts.Include(
            "~" + Paths.Scripts.signalr.jquery_signalR_2_0_0_min_js,
            "~" + Paths.Scripts.signalr.hubs_js,
            "~" + Paths.Scripts.liveControls.connectionManager_js,
            "~" + Paths.Scripts.liveControls.liveControls_js,
            "~" + Paths.Scripts.liveControls.liveTables_js
            );

        bundles.Add(bundleStyles);
        bundles.Add(bundleScripts);
        bundles.Add(bundleLiveScripts);

        BundleTable.EnableOptimizations = true;

    }

例外:

[NullReferenceException: 对象引用未设置为 对象。] System.Web.HttpContext.RequestRequiresAuthorization() +18 System.Web.Caching.OutputCacheModule.OnLeave(Object source, EventArgs eventArgs) +9606196
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

【问题讨论】:

  • 这听上去好像和bundling没什么关系,只是在用户未登录的情况下获取资源有问题(资源为null)。
  • @MikeSmithDev,为什么它是空的?为什么不是用户登录时?
  • 这是您的代码的问题。如果用户未登录和/或用户不是管理员,我猜它会像 Paths.Scripts.AdminSkin is null。
  • 我找到了问题的根源。感谢您的帮助。

标签: asp.net webforms bundling-and-minification authorize-attribute


【解决方案1】:

我在Global.ascx.cs 中使用过Application_PostAuthenticateRequest 并将HttpContext.Current.User 设为空。我已经评论了那行,问题就解决了。

public class Global : HttpApplication
{
    FormsAuthenticationTicket ticket;
    ExtendedUser extendedUser;

    protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
    {
       //cause of problem
        //HttpContext.Current.User = null;
        if (FormsAuthentication.CookiesSupported)
        {
            if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                try
                {
                    ticket = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value);

                    using (UnitOfWork unit = new UnitOfWork())
                    {
                        if (ticket.Name == SecurityHelpers.SuperAdminUserName)
                        {
                            SecurityHelpers.GetSuperAdminUser(unit);
                        }
                        else
                        {
                            extendedUser = SecurityHelpers.GetUser(unit, ticket);
                        }
                    }

                    if (extendedUser != null)
                    {
                        string[] roles = extendedUser.Group.GroupRoles.Select(a => a.Role.RoleName).ToArray();
                        HttpContext.Current.User = new GenericPrincipal(new MyFormsIdentity(extendedUser), roles);
                    }
                }
                catch (Exception)
                {
                    // Decrypt method failed.
                }
            }
        }
        else
        {
            //  throw new HttpException("Cookieless Forms Authentication is not " +
            //                          "supported for this application.");
        }
    }
}

【讨论】:

  • 太棒了!我花了一整天的时间试图从其他人的“自定义授权”代码中解决这个问题......他们将用户设置为 null 到处都是。删除了所有代码,它工作得很好。捆绑包在注销时完美加载,但无法查看锁定页面。好东西!这是一个很难找到的......最终梳理了异常和 mvc 捆绑包让我找到了这个 +shisha!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2013-01-21
  • 1970-01-01
相关资源
最近更新 更多