また、ビューに関連するjsファイルをビューと同じフォルダーに配置したいと考えていました。
私はこのスレッドの他のソリューションを機能させることができませんでした。それらが壊れているわけではありませんが、MVCを使い慣れていないため、機能させることができません。
ここに記載されている情報と他のいくつかのスタックを使用して、私は次のようなソリューションを考え出しました:
- JavaScriptファイルを、それが関連付けられているビューと同じディレクトリに配置できるようにします。
- スクリプトURLは基礎となる物理的なサイト構造を提供しません
- スクリプトURLの末尾にスラッシュ(/)を付ける必要はありません。
- 静的リソースに干渉しません。例:/Scripts/someFile.jsは引き続き機能します
- runAllManagedModulesForAllRequestsを有効にする必要はありません。
注:HTTP属性ルーティングも使用しています。これを有効にしなくても、私の魂で使用されているルートが変更されて機能する可能性があります。
次のディレクトリ/ファイル構造の例を考えます。
Controllers
-- Example
-- ExampleController.vb
Views
-- Example
-- Test.vbhtml
-- Test.js
下記の設定手順を上記の例の構造と組み合わせて使用すると、テストビューのURLは次のようにアクセスされ/Example/Test
、JavaScriptファイルは次のように参照されます。/Example/Scripts/test.js
ステップ1-属性ルーティングを有効にする:
/App_start/RouteConfig.vbファイルを編集してroutes.MapMvcAttributeRoutes()
、既存のルートのすぐ上に追加します。MapRoute:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports System.Web.Routing
Public Module RouteConfig
Public Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
' Enable HTTP atribute routing
routes.MapMvcAttributeRoutes()
routes.MapRoute(
name:="Default",
url:="{controller}/{action}/{id}",
defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}
)
End Sub
End Module
手順2-/{controller}/Scripts/*.jsを静的リソースではなくMVCパスとして扱い、処理するようにサイトを構成する
/Web.configファイルを編集し、ファイルのsystem.webServer->ハンドラーセクションに以下を追加します。
<add name="ApiURIs-ISAPI-Integrated-4.0" path="*/scripts/*.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
ここでもコンテキストを使用しています:
<system.webServer>
<modules>
<remove name="TelemetryCorrelationHttpModule"/>
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="managedHandler"/>
<remove name="ApplicationInsightsWebTracking"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler"/>
</modules>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
<remove name="OPTIONSVerbHandler"/>
<remove name="TRACEVerbHandler"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
<add name="ApiURIs-ISAPI-Integrated-4.0" path="*/scripts/*.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
手順3-次のスクリプトアクションの結果をコントローラーファイルに追加します
- ルートパスを編集して、コントローラーの{controller}名と一致するようにしてください。この例では、次のようになります。<Route( " Example / Scripts / {filename}")>
これを各コントローラーファイルにコピーする必要があります。必要に応じて、これを単一の1回限りのルート構成として何らかの方法で実行する方法がおそらくあります。
' /Example/Scripts/*.js
<Route("Example/Scripts/{filename}")>
Function Scripts(filename As String) As ActionResult
' ControllerName could be hardcoded but doing it this way allows for copy/pasting this code block into other controllers without having to edit
Dim ControllerName As String = System.Web.HttpContext.Current.Request.RequestContext.RouteData.Values("controller").ToString()
' the real file path
Dim filePath As String = Server.MapPath("~/Views/" & ControllerName & "/" & filename)
' send the file contents back
Return Content(System.IO.File.ReadAllText(filePath), "text/javascript")
End Function
コンテキストでは、これは私のExampleController.vbファイルです。
Imports System.Web.Mvc
Namespace myAppName
Public Class ExampleController
Inherits Controller
' /Example/Test
Function Test() As ActionResult
Return View()
End Function
' /Example/Scripts/*.js
<Route("Example/Scripts/{filename}")>
Function Scripts(filename As String) As ActionResult
' ControllerName could be hardcoded but doing it this way allows for copy/pasting this code block into other controllers without having to edit
Dim ControllerName As String = System.Web.HttpContext.Current.Request.RequestContext.RouteData.Values("controller").ToString()
' the real file path
Dim filePath As String = Server.MapPath("~/Views/" & ControllerName & "/" & filename)
' send the file contents back
Return Content(System.IO.File.ReadAllText(filePath), "text/javascript")
End Function
End Class
End Namespace
最後の注記
test.vbhtml view / test.js javascriptファイルについて特別なことは何もないため、ここには示されていません。
私はCSSをビューファイルに保持していますが、同じ方法でCSSファイルを参照できるように、このソリューションに簡単に追加できます。