Zohaib
Zohaib

Reputation: 496

Static Content on Development and Production Envoirnment

I am using Asp.Net MVC 2 to build the web application. The question is regarding the static content. In the production, the static content is residing at sub domain http://static.jobsora.com/content/css/. While, in development it is residing at the default location of ../Content/css/. Example:

For Production:

<link rel="stylesheet" type="text/css" href="http://static.jobsora.com/Content/css/search-min.css" />

For Development:

<link rel="stylesheet" type="text/css" href="<%= Url.Content("~/Content/css/search-min.css") %>" />

I know change of codebase for production is not at all an approach. So, I am looking for better approach. I think this can be trick down by Absolute URL, but how? No idea!

Thanks.

Upvotes: 0

Views: 621

Answers (2)

37Stars
37Stars

Reputation: 2489

Create a custom helper like this:

public static class HtmlHelpers
{
    public static MvcHtmlString Css(this HtmlHelper helper, string fileName)
    {
        string folder = ConfigurationManager.AppSettings["StaticContent"];
        fileName += (fileName.EndsWith(".css") == true) ? "" : ".css";
        return MvcHtmlString.Create(string.Format(@"<link rel=""stylesheet"" type=""text/css"" href=""{0}/{1}"" />", folder, helper.AttributeEncode(fileName)));
    }
}

Then in your View all you need is:

@Html.Css("search-min.css")

Then you could use web.config transformation to set the value for development and production. You can find information here, http://msdn.microsoft.com/en-us/library/dd465326.aspx.

Upvotes: 1

Brian Mains
Brian Mains

Reputation: 50728

Consider programmably adding links to the page by adding link elements to the head. It can be done from code-behind, and you could work around it that way using:

#if DEBUG

#else

#endif

Or some other construct.

HTH.

Upvotes: 1

Related Questions