I'm using portable areas in an MVC 2 application. I have a javascript file in a folder /Scripts/ViewModels/ViewModel.js but when I try to access it, I get a 404 error for not found. The registration for the portable area looks like this:
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" }
);
}
In a view template I tried to include the scripts file with
<script src="@Url.Content("~/Demo/Scripts/ViewModels/ViewModel.js")"></script>
and I get the following url /Demo/Scripts/ViewModels/ViewModel.js but the file is not accessable. I can only access script files which are direct children of the Scripts folder.
from the MSDN Documentation on routing,
so, you should try adding an
*to the beginning of the argument for your scripts path, eg,base.AreaRoutePrefix + "/Scripts/{*resourceName}",which would allow for the parameter to be handled as a catch-all. TheresourceNamewould then be able to be assigned to either/ViewModel.jsor/ViewModels/ViewModel.js. without the*, the/ViewModelssegment is treated as another segment, which causes the Routing Engine to not evaluate this route as a match, and continue through the defined routes, and the404is a result of no routes matching the pattern of the URL path provided.