开发者

Static Content on Development and Production Envoirnment

开发者 https://www.devze.com 2023-03-16 10:42 出处:网络
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.jobsor

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号