ASP.NET MVC 4 包给出 404 错误

标签 asp.net asp.net-mvc-4 bundling-and-minification umbraco6 system.web.optimization

我想将 Bundle 添加到现有的 ASP.NET MVC 4 (.NET 4.5) 网站中,该网站使用:

  • Umbraco 6.1.5
  • Microsoft ASP.NET Web 优化框架 1.1.3 (https://www.nuget.org/packages/microsoft.aspnet.web.optimization/)

  • 我尝试遵循以下说明: https://gist.github.com/jkarsrud/5143239 ,并且在我开始使用 bundles 路径之前,CSS 加载得很好。

    在页面加载时,它会插入样式引用:
    <link href="/bundles/marketingcss" rel="stylesheet">
    

    但是会发生 404 错误:
    > GET http://localhost:20459/bundles/marketingcss 404 (Not Found) 
    

    这是我在代码中的内容:

    Web.Config
    <add key="umbracoReservedPaths" value="~/umbraco,~/install/,~/bundles" />
    

    Global.asax
    <%@ Application Codebehind="Global.asax.cs" Inherits="MapCom.Global" Language="C#" %>
    

    Global.asax.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Optimization;
    using System.Web.Security;
    using System.Web.SessionState;
    using Umbraco.Web;
    
    namespace MapCom
    {
        public class Global : UmbracoApplication
        {
            protected void Application_Start(object sender, EventArgs e)
            {
                base.OnApplicationStarted(sender, e);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }
    }
    

    _Layout.cshtml
    @Styles.Render("~/bundles/marketingcss");
    

    BundleConfig.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Optimization;
    
    namespace MapCom
    {
        public class BundleConfig
        {
            public static void RegisterBundles(BundleCollection bundles)
            {
                BundleTable.EnableOptimizations = true;
                ///
                ///Marketing Site CSS Bundle
                ///
                bundles.Add(new StyleBundle("~/bundles/marketingcss")
                    .Include("~/plugins/bootstrap/css/bootstrap.min.css")
                    .Include("~/css/font-awesome.min.css")
                    .Include("~/plugins/parallax-slider/css/parallax-slider.css")
                    .Include("~/css/combinedStyles.min.css")
                    .Include("~/plugins/ladda-buttons/css/ladda.min.css")
                    .Include("~/plugins/ladda-buttons/css/custom-lada-btn.css"));
            }
        }
    }
    

    有任何想法吗?

    最佳答案

    在深入研究 Umbraco 之后,我注意到我正在进行的项目正在使用一个类:

     public class ApplicationEventHandler : IApplicationEventHandler
    

    这里有更详细的解释:http://our.umbraco.org/documentation/Reference/Events/application-startup

    但要点是:

    In order to bind to certain events in the Umbraco application you need to make these registrations during application startup. Based on the Umbraco version you are using there are various ways to hook in to the application starting. The higher the version you are using the more robust this becomes.



    所以 Umbraco 覆盖了一些 ASP.NET 启动方法。

    我通过在 ApplicationEventHandler.cs 中添加对 System.Web.Optimization 的引用解决了我的问题,并将我的包注册移动到该类中的此方法:
    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
    

    关于ASP.NET MVC 4 包给出 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24048293/

    相关文章:

    asp.net - asp.net mvc中如何知道请求是否是ajax?

    asp.net - 您是否需要在断开连接时手动从 SignalR 组中删除连接?

    visual-studio-2010 - 将 MVC 2 ASPX 转换为 MVC 4 Razor View 引擎

    asp.net-mvc-4 - 忽略包中的单个文件

    asp.net - IItemTransform 和现有的缩小文件

    asp.net-mvc-4 - Chrome 中的捆绑和缩小问题,但在 Mozilla 中运行良好

    javascript - 客户端id方法javascript

    asp.net - 在 ASP.NET 中;如何使用 ASP.NET 成员资格从远程 HTML 页面登录到另一个 ASPX 页面

    asp.net-mvc - ASP.NET MVC 4 - 我应该如何加密生产 SQL 服务器的连接字符串?

    javascript - Webpack Uglify 导致路由停止工作