MVC3领域的相对内容路径

时间:2022-11-07 04:05:39

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?

我发现这个问题不能使用ASP.NET MVC 2中的区域的相对路径,这与我遇到的问题相同。在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.

我决定使用上面问题中的解决方案和下面的解决方案(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 + "/"));
      }

    }
 }

5 个解决方案

#1


4  

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

你可以尝试在HtmlHelper上创建一个扩展来解决这个问题:

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.

如果我错了,我没有尝试代码,所以纠正我。

#2


18  

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..

我个人喜欢扩展方法路由,基于我提出的第一个答案,并测试它是否有效......而不是使用@ Url.Content,使用@ Url.ContentArea而不需要输入'〜/', '/'或'../'等。

The helper does some checking to automatically remove these, so just use is like this ...

帮助器做了一些检查以自动删除这些,所以只需使用就像这样......

@Url.ContentArea("Content/style.css") or @Url.ContentArea("Images/someimage.png") :)

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.

当您创建此Url Helper Extension时,您可以选择,但我在Web的根目录下创建了一个“Helpers”文件夹,然后我包含了@using YourWebNameSpace.Helpers;在我的_Layout.cshtml(razor / masterpage)中我所在的“区域”。

You can still use @Url.Content for references outside the current area (basically you can mix based on the resource needed and its location).

您仍然可以将@ Url.Content用于当前区域之外的引用(基本上您可以根据所需资源及其位置进行混合)。

namespace YourWebNamespace.Helpers
{
    public static class UrlHelperExtensions
    {
        public static string ContentArea(this UrlHelper url, string path)
        {
            var area = url.RequestContext.RouteData.DataTokens["area"];

            if (area != null)
            {
                if (!string.IsNullOrEmpty(area.ToString()))
                    area = "Areas/" + area;

                // Simple checks for '~/' and '/' at the
                // beginning of the path.
                if (path.StartsWith("~/"))
                    path = path.Remove(0, 2);

                if (path.StartsWith("/"))
                    path = path.Remove(0, 1);

                path = path.Replace("../", string.Empty);

                return VirtualPathUtility.ToAbsolute("~/" + area + "/" + path);
            }

            return string.Empty;
        }
    }
}

in your master page, or any page (Razor only for this example) ...

在您的母版页或任何页面中(仅适用于此示例的Razor)...

@using YourWebNamespace.Helpers

<html lang="en">
<head>

    <link href="@Url.ContentArea("Content/reset.css")" media="screen" rel="stylesheet" type="text/css" />

    <link href="@Url.ContentArea("Content/style.css")" media="screen" rel="stylesheet" type="text/css" />

</head>
<body>

#3


2  

I just had a similar problem in a style sheet that I solved by just putting in both relative paths:

我在样式表中遇到了类似的问题,我只需要放入两个相对路径即可解决:

#searchbox {
    ...
    background: url("../Content/magglass.png") no-repeat;
    background: url("../../Content/magglass.png") no-repeat;
    ...
}

#4


1  

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:

我在上面的UrlHelperExtensions中尝试了一些我在母版页中用作jQuery滚动横幅数据的图像。我不得不添加以下else子句,以便当没有区域时帮助器将返回tha路径:

        else
        {
            if (path.StartsWith(@"~/"))
            {
                var result = VirtualPathUtility.ToAbsolute(path);
                return result;
            }
        }

        return string.Empty;

In my view page I used:

在我的视图页面中,我使用了:

<img src="@Url.ContentArea("~/images/ScrollPicture1.png")" alt="Gallery picture 1" />

Thanks for posting this solution. Works great with my fix for images.

感谢您发布此解决方案。适用于我的图像修复工作。

#5


-1  

I had similar problems with my ASP.NET MVC 3 web application and I fixed it writing instead of

我的ASP.NET MVC 3 Web应用程序遇到了类似的问题,我修改了它而不是

<img src="mypath/myimage.png" alt="my alt text" />

this:

这个:

<img src='@Url.Content("mylink")' alt="my alt text" />

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...

最后一个mylink是〜/ mypath之类的路径。我用它来链接我的javascript文件,但我没有尝试使用图片或链接,所以你应该尝试自己...

#1


4  

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

你可以尝试在HtmlHelper上创建一个扩展来解决这个问题:

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.

如果我错了,我没有尝试代码,所以纠正我。

#2


18  

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..

我个人喜欢扩展方法路由,基于我提出的第一个答案,并测试它是否有效......而不是使用@ Url.Content,使用@ Url.ContentArea而不需要输入'〜/', '/'或'../'等。

The helper does some checking to automatically remove these, so just use is like this ...

帮助器做了一些检查以自动删除这些,所以只需使用就像这样......

@Url.ContentArea("Content/style.css") or @Url.ContentArea("Images/someimage.png") :)

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.

当您创建此Url Helper Extension时,您可以选择,但我在Web的根目录下创建了一个“Helpers”文件夹,然后我包含了@using YourWebNameSpace.Helpers;在我的_Layout.cshtml(razor / masterpage)中我所在的“区域”。

You can still use @Url.Content for references outside the current area (basically you can mix based on the resource needed and its location).

您仍然可以将@ Url.Content用于当前区域之外的引用(基本上您可以根据所需资源及其位置进行混合)。

namespace YourWebNamespace.Helpers
{
    public static class UrlHelperExtensions
    {
        public static string ContentArea(this UrlHelper url, string path)
        {
            var area = url.RequestContext.RouteData.DataTokens["area"];

            if (area != null)
            {
                if (!string.IsNullOrEmpty(area.ToString()))
                    area = "Areas/" + area;

                // Simple checks for '~/' and '/' at the
                // beginning of the path.
                if (path.StartsWith("~/"))
                    path = path.Remove(0, 2);

                if (path.StartsWith("/"))
                    path = path.Remove(0, 1);

                path = path.Replace("../", string.Empty);

                return VirtualPathUtility.ToAbsolute("~/" + area + "/" + path);
            }

            return string.Empty;
        }
    }
}

in your master page, or any page (Razor only for this example) ...

在您的母版页或任何页面中(仅适用于此示例的Razor)...

@using YourWebNamespace.Helpers

<html lang="en">
<head>

    <link href="@Url.ContentArea("Content/reset.css")" media="screen" rel="stylesheet" type="text/css" />

    <link href="@Url.ContentArea("Content/style.css")" media="screen" rel="stylesheet" type="text/css" />

</head>
<body>

#3


2  

I just had a similar problem in a style sheet that I solved by just putting in both relative paths:

我在样式表中遇到了类似的问题,我只需要放入两个相对路径即可解决:

#searchbox {
    ...
    background: url("../Content/magglass.png") no-repeat;
    background: url("../../Content/magglass.png") no-repeat;
    ...
}

#4


1  

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:

我在上面的UrlHelperExtensions中尝试了一些我在母版页中用作jQuery滚动横幅数据的图像。我不得不添加以下else子句,以便当没有区域时帮助器将返回tha路径:

        else
        {
            if (path.StartsWith(@"~/"))
            {
                var result = VirtualPathUtility.ToAbsolute(path);
                return result;
            }
        }

        return string.Empty;

In my view page I used:

在我的视图页面中,我使用了:

<img src="@Url.ContentArea("~/images/ScrollPicture1.png")" alt="Gallery picture 1" />

Thanks for posting this solution. Works great with my fix for images.

感谢您发布此解决方案。适用于我的图像修复工作。

#5


-1  

I had similar problems with my ASP.NET MVC 3 web application and I fixed it writing instead of

我的ASP.NET MVC 3 Web应用程序遇到了类似的问题,我修改了它而不是

<img src="mypath/myimage.png" alt="my alt text" />

this:

这个:

<img src='@Url.Content("mylink")' alt="my alt text" />

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...

最后一个mylink是〜/ mypath之类的路径。我用它来链接我的javascript文件,但我没有尝试使用图片或链接,所以你应该尝试自己...