.Net MVC4 發佈後頁面樣式問題

  1. 樣式目錄由於限制登錄未能被加載
    如果網站限制必須登錄才能訪問網站(例:後臺系統),必須在根目錄Web.config配置資源目錄無需登錄.

     <!--頁面權限控制-->
            <configuration>
              <location path="Content">
                <system.web>
                  <authorization>
                    <allow users="*"/>
                  </authorization>
                </system.web>
              </location>
              <location path="Scripts">
                <system.web>
                  <authorization>
                    <allow users="*"/>
                  </authorization>
                </system.web>
              </location>
    
              <!-- 注意:這個目錄雖然項目沒這個目錄,但是由於BundleConfig中設置js集目錄別名
              路徑爲bundles開頭,所以發佈後頁面源碼中路徑也是bundles,所以要把這個目錄設置爲
              免登陸 -->
              <location path="bundles">
                <system.web>
                  <authorization>
                    <allow users="*"/>
                  </authorization>
                </system.web>
              </location>
              <location path="Images">
                <system.web>
                  <authorization>
                    <allow users="*"/>
                  </authorization>
                </system.web>
              </location>
            </configuration>

    注意: 由於BundleConfig中設置的問題,可能雖然js在Scripts目錄中,但是設置的別名路徑爲其他名,則需要把這個別名路徑第一個目錄名設置爲免登陸.

    Example:

    BundleConfig中設置的路徑別名:
    BundleConfig中設置的路徑別名

    發佈後頁面源碼顯示:
    發佈後頁面源碼顯示

    所以需要設置bundles目錄爲免登陸

  2. CSS樣式出錯
    查看頁面源碼,在css頂部出現以下提示:

        /* 未能縮小。正在返回未縮小的內容。
        (2,2-3): run-time warning JS1195: Expected expression: .
        (2,36-37): run-time warning JS1004: Expected ';': :
        (2,85-86): run-time warning JS1004: Expected ';': :
        (2,139-140): run-time warning JS1004: Expected ';': :
        (2,182-183): run-time warning JS1197: Too many errors. The file might not be a JavaScript file: :
     */

    查看系統源碼發現BundleConfig中設置導致,css樣式用了ScriptBundle導致壓縮出錯.

    正確格式:
    JS:

     bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                            "~/Scripts/jquery-{version}.js"));

    CSS:

    bundles.Add(new StyleBundle("~/Content/css/login").Include(
                    "~/Content/global.css",
                    "~/Content/login.css"));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章