2015-09-06 20 views
7

に配備されたときに多くの質問がありますが、この1のようにstackoverflowの中で尋ねたと答え:MVCバンドルと相対CSSの画像のウェブサイトが絶対パスに相対的な画像のパスを変換するにはアプリケーション

MVC4 StyleBundle not resolving images

new CssRewriteUrlTransform()を追加することをお勧めしています以下のようになります。

bundles.Add(new StyleBundle("~/Content/css/jquery-ui/bundle") 
     .Include("~/Content/css/jquery-ui/*.css", new CssRewriteUrlTransform())); 

これは実際に私を救ってくれました。しかし、今、問題がまだある私は、アプリケーション(ウェブサイトのないルート)に私のウェブサイトを展開することを:

http://localhost/sample/ 

が、画像のURLは次のようです::

アプリケーションです

http://localhost/css/imgs/spirit.png 

それがされるべきである:

http://localhost/sample/css/imgs/spirit.png 

バンドルCSSのリンク自体は正しいと作業ですが。

+0

サンプルはIISの仮想ディレクトリですか? –

+0

これはIISのアプリケーションと仮想ディレクトリです。このサンプルでは、​​アプリケーション名は** sample ** –

+0

です。http://stackoverflow.com/questions/19765238/cssrewriteurltransform-with-or-without-virtual-directoryを見てください。何が起こったのか、そして何が必要なのかを完全に記述しています。 *する。 –

答えて

4

一切の仮想ディレクトリが存在しない場合は、次のコードはどうしたら関与:

bundles.Add(new StyleBundle("~/bundles/css").Include(
       "~/Content/css/*.css", new CssRewriteUrlTransform())); 

しかしれるVirtualDirectoryが使用されている場合CssRewriteUrlTransformではなく、ホスト/のVirtualDirectoryをホストするために書き換えます。ソリューションは、ここで詳しく説明するCssRewriteUrlTransformを導出することです。ASP.NET MVC4 Bundling with Twitter Bootstrap

public class CssRewriteUrlTransformWrapper : IItemTransform 
{ 
    public string Process(string includedVirtualPath, string input) 
    {   
     return new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);   
    } 
} 
1

これは実際にアプリを仮想ディレクトリとして実行されている場合、正しく動作していない 一般的に最適化してちょうど別の問題です。あなたのウェブサイトをホストするための仮想ディレクトリを使用している、あなたは固定パスでCssRewriteUrlTransformを呼び出すラッパーをしなければならないので

public class CssRewriteUrlTransformWrapper : IItemTransform 
{ 
     public string Process(string includedVirtualPath, string input) 
     {   
      return new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);   
     } 
} 

More info

関連する問題