, ASP.NET( MVC) , . "", . MVC ASP.NET 5, . , .
:
public static class ScriptHelper
{
public static IHtmlString Render(params string[] paths)
{
return RenderFormat(DefaultTagFormat, paths);
}
public static IHtmlString RenderFormat(string tagFormat, params string[] paths)
{
if (string.IsNullOrEmpty(tagFormat))
{
throw new ArgumentException("Tag Format cannot be null");
}
if (paths == null)
{
throw new ArgumentNullException("Paths cannot be empty");
}
if (paths.Any(string.IsNullOrEmpty))
{
throw new ArgumentException("paths");
}
return BuildHtml(tagFormat, paths);
}
private static string defaultFormat = "<script src=\"{0}?ver={1}\"></script>";
public static string DefaultTagFormat
{
get
{
return defaultFormat;
}
set { defaultFormat = value; }
}
private static IHtmlString BuildHtml(string tagFormat, params string[] paths)
{
StringBuilder builder = new StringBuilder();
foreach (string path in paths)
{
StringBuilder builder = new StringBuilder();
foreach (string path in paths)
{
var version = "1234";
var fileToOutPut = new VersionedJsFile(path,version,tagFormat);
builder.Append(fileToOutPut.RenderOutput());
builder.Append(Environment.NewLine);
}
return new HtmlString(builder.ToString());
}
}
, , .. , ( ) . . "VersionHelper" css/js/etc.
internal abstract class HtmlFile
{
public abstract string RenderOutput();
}
internal class VersionedJsFile : HtmlFile
{
private string _version;
private string _path;
private string _format;
public VersionedJsFile(
string path,
string version,
string format)
{
if (version != null) _version = version;
if (path != null) _path = path;
if(!string.IsNullOrEmpty(format))
_format = format;
}
public override string RenderOutput()
{
if (!string.IsNullOrEmpty(_path)
&& !string.IsNullOrEmpty(_format))
{
string versionedFilePath = string.Format(_format, _path, _version);
return versionedFilePath;
}
return string.Empty;
}
}
Then add an assistant to the page / main page / page layout:
<%: ScriptHelper.Render("/scripts/bootstrap.js") %>
As you can see, the helper accepts a params object, so you can immediately apply to multiple files.
Output:
<script src="/scripts/bootstrap.js?ver=1234"></script>
Assistant can be easily enhanced.
source
share