Tôi có một tập hợp các phương pháp mở rộng tôi sử dụng (xem bên dưới). Bạn có thể sử dụng chúng làm cơ sở/ví dụ để tạo các phương pháp mở rộng/gỡ lỗi tùy chỉnh của riêng bạn.
chung debug/release:
public static MvcHtmlString DebugReleaseString(this System.Web.Mvc.HtmlHelper html, string debugString, string releaseString)
{
string toReturn = debugString;
#if !DEBUG
if (!string.IsNullOrEmpty(releaseString))
toReturn = releaseString;
#endif
return MvcHtmlString.Create(toReturn);
}
chung sử dụng debug/release:
@Html.DebugReleaseString("/images/myimage.jpg", "http://mycdn.com/images/myimage.jpg")
gỡ lỗi thẻ/release CSS:
public static MvcHtmlString CssTag(this System.Web.Mvc.HtmlHelper html, string fileName)
{
return html.CssTag(fileName, string.Empty);
}
public static MvcHtmlString CssTag(this System.Web.Mvc.HtmlHelper html, string fileName, string releaseFileName)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException("fileName");
string cssTag = string.Format(
"<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />",
html.MeDebugReleaseString(fileName, releaseFileName));
return MvcHtmlString.Create(cssTag);
}
Debug/phát hành thẻ CSS sử dụng:
@Html.CssTag("/styles/mystyle.css")
@Html.CssTag("/styles/mystyle.css", "http://mycdn.com/styles/mystyle.css")
Debug/phát hành thẻ JS:
public static MvcHtmlString JavascriptTag(this System.Web.Mvc.HtmlHelper html, string fileName)
{
return html.JavascriptTag(fileName, string.Empty);
}
public static MvcHtmlString JavascriptTag(this System.Web.Mvc.HtmlHelper html, string fileName, string releaseFileName)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException("fileName");
string jsTag = string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",
html.MeDebugReleaseString(fileName, releaseFileName));
return MvcHtmlString.Create(jsTag);
}
Debug/phát hành JS thẻ sử dụng:
@Html.JavascriptTag("/scripts/myscript.css")
@Html.JavascriptTag("/scripts/myscript.css", "http://mycdn.com/scripts/myscript.js")
bổ sung tùy chọn debug/release:
public enum RenderModeEnum
{
Debug,
Release,
DebugAndRelease
}
public static MvcHtmlString CssTag(this System.Web.Mvc.HtmlHelper html, string fileName, RenderModeEnum tagRenderMode)
{
if (tagRenderMode == RenderModeEnum.DebugAndRelease)
return html.CssTag(fileName);
#if DEBUG
if (tagRenderMode == RenderModeEnum.Debug)
return html.CssTag(fileName);
#else
if (tagRenderMode == RenderModeEnum.Release)
return html.CssTag(fileName);
#endif
return MvcHtmlString.Empty;
}
public static MvcHtmlString JavascriptTag(this System.Web.Mvc.HtmlHelper html, string fileName, RenderModeEnum tagRenderMode)
{
if (tagRenderMode == RenderModeEnum.DebugAndRelease)
return html.JavascriptTag(fileName);
#if DEBUG
if (tagRenderMode == RenderModeEnum.Debug)
return html.JavascriptTag(fileName);
#else
if (tagRenderMode == RenderModeEnum.Release)
return html.JavascriptTag(fileName);
#endif
return MvcHtmlString.Empty;
}
bổ sung debug/release tùy chọn sử dụng:
@Html.CssTag("/styles/mystyle.css", RenderModeEnum.DebugAndRelease)
@Html.CssTag("/styles/mystyle.css", RenderModeEnum.Debug)
@Html.CssTag("http://mycdn.com/styles/mystyle.css", RenderModeEnum.Release)
Điều đó thật tuyệt vời. Cảm ơn bạn. – stephen776
@ stephen776, câu trả lời được cập nhật với phương pháp mở rộng –
@ stephen776 nếu bạn nhìn vào mã nguồn, nó được thiết lập để hoạt động mà không có giá trị web.config. Tôi bỏ qua các ứng dụng cdnroot khi tôi đang phát triển tại địa phương, sau đó có biến đổi cấu hình web của tôi thêm nó vào lúc xây dựng. –