我还想把与视图相关的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.MapRoute 上方添加 routes.MapMvcAttributeRoutes():
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 --> handlers 部分:
<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 文件中
- 确保编辑路由路径以匹配 {controller} 名称
控制器,对于这个例子,它是:
示例/Scripts/{filename}")>
-
您需要将其复制到您的每个控制器文件中。如果您愿意,可能有一种方法可以以某种方式将其作为单一的、一次性的路由配置来完成。
' /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 视图/test.js javascript 文件没有什么特别之处,此处未显示。
我将我的 CSS 保存在视图文件中,但您可以轻松地添加到此解决方案中,以便您可以以类似的方式引用您的 CSS 文件。