MVC3 区域中的相对内容路径
我发现这个问题 Can't userelative paths ASP.NET MVC 2 中的区域与我遇到的问题相同。 MVC3 中还是这样吗?
有没有办法将内容文件保存在相对于该区域的区域中?
因此,区域中的布局文件可以具有类似的内容,
无需创建完全限定的链接,需要区域目录和区域名称,或者上述问题的解决方案,需要在每个请求上检查每个区域。
更新/编辑
我决定使用上述问题中的解决方案和下面的解决方案(html 帮助程序) - 根据项目/情况。我对上述内容的实现使用 app.setting 来存储区域名称和扩展名,以便我可以将该模块作为我的库的一部分。
var context = HttpContext.Current;
var path = context.Request.Path;
var list = ... //code that gets from app.config and then saves it
var extensions = ... // to the cache as non-removable with a dependency on web.config
foreach (var area in list)
{
if (!path.Contains(area + "/")) continue;
foreach (var extension in extensions)
{
if (path.EndsWith("." + extension))
{
context.RewritePath(path.Replace(area + "/", "Areas/" + area + "/"));
}
}
}
I found this question Can't use relative paths with areas in ASP.NET MVC 2 which is the same issue I am having. Is this still the case in MVC3?
Is there a way to keep content files in an area relative to the area?
So that a layout file in an area can have something like
Without having to either make a fully qualified link, requiring the areas directory and the area name or the solution of the above question which requires a check for each area on each request.
update/edit
I've decided to use both the solution in the above question and the one below (html helper) - depending on the project/situation. My implementation of the above uses app.setting to store the area names and the extensions so that I can just have the module as part of my library.
var context = HttpContext.Current;
var path = context.Request.Path;
var list = ... //code that gets from app.config and then saves it
var extensions = ... // to the cache as non-removable with a dependency on web.config
foreach (var area in list)
{
if (!path.Contains(area + "/")) continue;
foreach (var extension in extensions)
{
if (path.EndsWith("." + extension))
{
context.RewritePath(path.Replace(area + "/", "Areas/" + area + "/"));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我个人喜欢扩展方法路由,基于我想出的第一个答案并测试它是否有效......而不是使用
@Url.Content
,而是使用@Url.ContentArea< /code> 并且不需要放入 '~/'、'/' 或 '../' 等。
助手会进行一些检查以自动删除这些内容,所以只需使用这样的...
当你创建这个时Url Helper 扩展,您可以选择,但我在网络根目录下创建了一个“Helpers”文件夹,然后将
@using YourWebNameSpace.Helpers;
包含在我的_Layout.cshtml
中(razor/masterpage) 在我所在的任何“区域”中。您仍然可以使用
@Url.Content
作为当前区域之外的引用(基本上您可以根据所需的资源及其位置进行混合)。在您的母版页或任何页面(仅适用于本示例的 Razor)...
I personally like the extension method route, based on the first answer I came up with this and tested that it works ... Instead of using
@Url.Content
, use@Url.ContentArea
and no need to put in '~/', '/' or '../', etc..The helper does some checking to automatically remove these, so just use is like this ...
When you create this Url Helper Extension, your choice, but I created a 'Helpers' folder off the root of the web then I include the
@using YourWebNameSpace.Helpers;
in my_Layout.cshtml
(razor/masterpage) in whatever 'Area' I am in.You can still use
@Url.Content
for references outside the current area (basically you can mix based on the resource needed and its location).in your master page, or any page (Razor only for this example) ...
您可以尝试在 HtmlHelper 上创建一个扩展来解决这个问题:
所以在您看来您可以使用:
我没有尝试该代码,所以如果我错了,请纠正我。
You can try creating an extention on HtmlHelper to work this out:
so in your view you can use:
I didn't try the code so correct me if I'm wrong.
我尝试了上面的 UrlHelperExtensions 来获取一些图像,这些图像在我的母版页中用作 jQuery 滚动横幅的数据。我必须添加以下 else 子句,以便助手在没有区域时返回路径:
在我的视图页面中我使用:
感谢发布此解决方案。非常适合我的图像修复。
I tried the UrlHelperExtensions above for some images which I use in my masterpage as data for a jQuery scrolling banner. I had to add the following else clause so that the helper would return tha path when there was no area:
In my view page I used:
Thanks for posting this solution. Works great with my fix for images.
我刚刚在样式表中遇到了类似的问题,我通过输入两个相对路径解决了这个问题:
I just had a similar problem in a style sheet that I solved by just putting in both relative paths:
我的 ASP.NET MVC 3 Web 应用程序也遇到了类似的问题,我修复了它,而不是这样写
:
其中最后一个 mylink 是类似 ~/mypath 的路径。我用它来链接我的 javascript 文件,但是我没有尝试使用图像或链接,所以你应该自己尝试......
I had similar problems with my ASP.NET MVC 3 web application and I fixed it writing instead of
this:
where the last mylink is a path like ~/mypath. I used it to link my javascript files, however I didn't try it with images or links so you should try yourself...