我在MVC 2應用程序中使用便攜式區域。我在文件夾 /Scripts/ViewModels/ViewModel.js中有一個JavaScript文件,但是當我嘗試訪問它時,我得到一個未找到的404錯誤。便攜式地區註冊看起來像這樣:如何設置MVC可移植區域以使用複雜目錄結構中的資源?
private void RegisterRoutes(AreaRegistrationContext context)
{
context.MapRoute(
AreaName + "_resources",
base.AreaRoutePrefix + "/resource/{resourceName}",
new { controller = "EmbeddedResource", action = "Index"},
new[] { "MvcContrib.PortableAreas" }
);
context.MapRoute(
AreaName + "_scripts",
base.AreaRoutePrefix + "/Scripts/{resourceName}",
new { controller = "EmbeddedResource", action = "Index", resourcePath = "scripts" },
new[] { "MvcContrib.PortableAreas" }
);
context.MapRoute(
AreaName + "_images",
base.AreaRoutePrefix + "/images/{resourceName}",
new { controller = "EmbeddedResource", action = "Index", resourcePath = "images" },
new[] { "MvcContrib.PortableAreas" }
);
context.MapRoute(
AreaName + "_default",
base.AreaRoutePrefix + "/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "PortableAreaDemo.PortableAreas.Areas.Demo.Controllers", "MvcContrib" }
);
}
在我試圖包括腳本與 <script src="@Url.Content("~/Demo/Scripts/ViewModels/ViewModel.js")"></script>
文件視圖模板,我得到了以下網址/Demo/Scripts/ViewModels/ViewModel.js
但該文件是不可訪問,。我只能訪問腳本文件,它們是Scripts文件夾的直接子項。
你的問題沒有意義。您建議您嘗試從'/ Scripts/ViewModels/ViewModel.js'加載文件,但是您聲明URL'/ Demo/Scripts/ViewModels/ViewModel.js'不起作用。是否需要路徑中的'/ Demo /'文件夾? – Claies
@Andrew:我確實需要路徑中的Demo,但這不是一個文件夾,而是該區域的名稱。問題是來自/ Demo/Scripts/ViewModels /文件夾的文件未加載。如果我把文件放在腳本文件夾中,那就沒問題。所以例如/Demo/Scripts/ViewModel.js加載正確。 – Zoliqa
好的,我很抱歉,我明顯誤解了你的問題......所以你的問題不在該區域,而是從腳本文件夾的子文件夾訪問文件。 – Claies