10

I created an area in my MVC solution called "Admin". In this area I created a folder named "Content" to store my css files.

I try to reference on of my css file (MaterialPacking.css) from a view (cshtml) like this:

enter image description here

<link href="@Url.Content("~/Areas/Admin/Content/MaterialPacking.css")" rel="stylesheet" type="text/css" />

Is this the good way?

Thanks.

Bronzato
  • 9,438
  • 29
  • 120
  • 212
  • You should try to visit css's link address from view source in separaete tab and check what error does it give – archil Mar 16 '12 at 12:13
  • 1
    this was answered here. check it out http://stackoverflow.com/questions/7495780/mvc-3-wont-serve-content-files-from-areas-subfolder – Jay Mar 16 '12 at 12:30
  • Does `@Url.Content("~/Content/MaterialPacking.css")` work? – jrummell Mar 16 '12 at 12:41
  • @jrummell: no @Url.Content("~/Content/MaterialPacking.css") doesn't work. – Bronzato Mar 17 '12 at 16:39

3 Answers3

5

that's really the only way to do it, unless you create a routed handler to grab it from the area folder

Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62
3

You create a class for example ConentUrlHelper.cs

namespace CrewNetix.helper
{
    public static class ContentUrlHelper
    { 
        public static string ContentArea(this UrlHelper url, string path)
        { 
            var modulName = url.RequestContext.RouteData.DataTokens["area"];
            string modulContentLoad = "";

            if (modulName != null)

            {
                if (!string.IsNullOrEmpty(modulName.ToString()))
                    modulContentLoad = "Areas/" + modulName;

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

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


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

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

            return string.Empty;
        }

    }
}

And in this way you can access files:

<script src="@Url.ContentArea("Script/PageLoad.js")" ></script>
<script src="@Url.ContentArea("Script/jquery-1.9.1.min.js")" ></script>
<script src="@Url.ContentArea("Script/kendo.all.min.js")" ></script>
<script src="@Url.ContentArea("Script/kendo.web.min.js")" ></script>
<link href="@Url.ContentArea("Content/Css/kendo.common.min.css")" rel="stylesheet" type="text/css" />
<link href="@Url.ContentArea("Content/Css/kendo.default.min.css")" rel="stylesheet" type="text/css" />
Koopakiller
  • 2,838
  • 3
  • 32
  • 47
Serdar Şengül
  • 77
  • 1
  • 1
  • 6
0

You can try use style from wwwroot

<link href="~/css/yourstyle.css" rel="stylesheet" type="text/css" />

It is work for ASP.NET Core 2