Bronzato
Bronzato

Reputation: 9342

Referencing a css file located in area

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.

Upvotes: 10

Views: 7435

Answers (3)

Mikhail121
Mikhail121

Reputation: 9

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

Upvotes: 0

Serdar Şeng&#252;l
Serdar Şeng&#252;l

Reputation: 77

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" />

Upvotes: 3

Anthony Shaw
Anthony Shaw

Reputation: 8166

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

Upvotes: 5

Related Questions