この回答には2つのアプローチが含まれています。2 番目の方がシナリオにより適している場合があります。最初のものは、一般的な mvc プロジェクトにより適している可能性があります
アプローチする
スクリプトと css ファイルを格納するために、コンテンツ フォルダーに整理された構造を作成することをお勧めします。つまり、
/Content/Demos/Page-Title-1/
/Content/Demos/Page-Title-2/
/Content/Demos/Page-Title-3/
と
/Content/Demos/Common/
次に、各ページ タイトルのスクリプトと css ファイルをレンダリングするためのバンドルを作成します。
すなわち。
bundles.Add(new StyleBundle("~/Demo/page-title/css").Include(
"~/Content/Demos/Page-Title-1/csscontent1.css",
"~/Content/Demos/Page-Title-1/csscontent2.css",
"~/Content/Demos/Page-Title-1/csscontent3.css",
"~/Content/Demos/Page-Title-1/csscontent4.css"));
bundles.Add(new StyleBundle("~/Demo/page-title/js").Include(
"~/Content/Demos/Page-Title-1/jscontent1.css",
"~/Content/Demos/Page-Title-1/jscontent2.css",
"~/Content/Demos/Page-Title-1/jscontent3.css",
"~/Content/Demos/Page-Title-1/jscontent4.css"));
これにより、数行のアプローチを使用してデモ ページでスクリプトをレンダリングできます。
@Styles.Render("~/Demo/page-title/css");
@Scripts.Render("~/Demo/page-title/jss");
@Styles.Render("~/Demo/common/css");
@Scripts.Render("~/Demo/common/css");
/Content/Demos/Page-Title/ フォルダー内のファイルを変更すると、グローバル .asax 内のファイルを更新する必要があります。必要に応じて、ファイルをバンドルおよび縮小して、最初のページの読み込みの帯域幅と読み込み時間を節約できるという利点があります。
2に近づく。
(引き続き、次のフォルダー構造を使用します。
/Content/Demos/Common/
と
/Content/Demos/Page-Title-1/
/Content/Demos/Page-Title-2/
/Content/Demos/Page-Title-3/)
フォルダー内のすべてのスクリプトとコンテンツを参照する html ヘルパーを作成する
その使用法は @Asset.RenderAssets( '~/folderdirectory') になります
そしてヘルパーは次のようなことをします
@helper RenderAssets (stirng directory){
@* scrape the directory for all script files*
var scripts = find all scripts in the directory
@* include the script files *@
for each script
<script src=" ... .js"></script>
@* scrape the directory for all cssfiles*
var styles = all css in the directory
@* include the css files *@
for each style
<link rel='stylesheet' type="text/css" href=" ... .css">
}
これは、各デモ ビューで数行使用することになります。
@Asset.RenderAssets( '~/Content/Demos/Common')
@Asset.RenderAssets( '~/Content/Demos/Page-Title')
これをglobal.asaxまたはRouteConfig.csファイルの追加の数行または2行と組み合わせる必要がある場合とない場合があります(ソース3を参照)
routes.IgnoreRoute("/Content/Demos/{page}/{script}.js");
routes.IgnoreRoute("/Content/Demos/{page}/{style}.css");
HTML ヘルパーを作成するための関連ソースについては、
http: //weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx を参照してください。
バンドルと縮小化 (scripts.render アプローチ) を使用するには、
http: //www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification を参照してください。
フィル・ハークは、これを無視ルートと組み合わせる必要はないかもしれないと言います!
https://stackoverflow.com/a/30551/1778606
コメントと編集をお勧めします。