在MVC3地区相对内容路径 [英] Relative Content Path in MVC3 Areas

查看:171
本文介绍了在MVC3地区相对内容路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个问题,<一个href=\"http://stackoverflow.com/questions/3397924/cant-use-relative-paths-with-areas-in-asp-net-mvc-2\">Can't使用ASP.NET MVC 2 这是我有同样的问题与地区相对路径。这是仍处于MVC3的情况?

有没有办法来保证内容的文件相对面积?

因此​​,在一个区域的布局文件可以有类似

而不必要么做一个完全合格的链接,要求区目录和地区名称或上述问题的解决方案,它需要对每个请求每个区域进行检查。

更新/修改

我已经决定同时使用在上述问题的解决方案和下面的一(HTML辅助) - 根据项目/局面。我的上述用途app.setting存储区域名称和扩展的实施,使我可以只具备模块作为我的图书馆的一部分。

  VAR背景= HttpContext.Current;
VAR路径= context.Request.Path;
VAR列表= ... // code,从app.config中获取并然后将其保存
VAR扩展= ... //缓存为不可移动与web.config中的依赖
的foreach(在列表VAR区域)
{
   如果(path.Contains(区+/)!)继续;
   的foreach(在扩展VAR扩展)
   {
      如果(path.EndsWith(。+扩展名))
      {
         context.RewritePath(path.Replace(区+/,地区/+区域+/));
      }    }
 }


解决方案

您可以尝试创建上的HtmlHelper的延伸工作了这一点:

 公共静态字符串映像(此HTML的HtmlHelper,字符串的ImagePath)
{
    VAR面积= html.ViewContext.RouteData.Values​​ [区];
    如果(!string.IsNullOrEmpty(面积))
        面积=地区/+区域+/;
    返回VirtualPathUtility.ToAbsolute(〜/+区域+内容/ IMG /+的ImagePath);
}

所以在你看来,你可以使用:

 &LT; IMG SRC =@ Html.Image(myimage.png),ALT =.../&GT;

我没有尝试在code所以纠正我,如果我错了。

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 + "/"));
      }

    }
 }

解决方案

You can try creating an extention on HtmlHelper to work this out:

public static string Image(this HtmlHelper html, string imagePath)
{
    var area = html.ViewContext.RouteData.Values["area"];
    if (!string.IsNullOrEmpty(area))
        area = "Areas/" + area + "/";
    return VirtualPathUtility.ToAbsolute("~/" + area + "Content/img/" + imagePath);
}

so in your view you can use:

<img src="@Html.Image("myimage.png")" alt="..." />

I didn't try the code so correct me if I'm wrong.

这篇关于在MVC3地区相对内容路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆