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:
<link href="@Url.Content("~/Areas/Admin/Content/MaterialPacking.css")" rel="stylesheet" type="text/css" />
Is this the good way?
Thanks.
that's really the only way to do it, unless you create a routed handler to grab it from the area folder
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With