Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing a css file located in area

Tags:

asp.net-mvc

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.

like image 490
Bronzato Avatar asked Mar 16 '12 11:03

Bronzato


2 Answers

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

like image 134
Anthony Shaw Avatar answered Jan 31 '23 10:01

Anthony Shaw


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" />
like image 27
Serdar Şengül Avatar answered Jan 31 '23 11:01

Serdar Şengül