استفاده از Razor در فایلهای JavaScript و CSS
نویسنده: سیروان عفیفی
تاریخ: ۱۳۹۴/۰۲/۳۰ ۱۶:۳۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public ActionResult Style()
{
Response.ContentType = "text/css";
var model = new Style
{
Color = "red",
Background = "blue"
};
return View(model);
} @model ExternalJavaScript.Models.Style
@{
Layout = null;
}
body {
color : @Model.Color;
background-color : @Model.Background;
} <link rel="stylesheet" href="@Url.Action("Style","Home")" /> public class ContentType : ActionFilterAttribute
{
private string _contentType;
public ContentType(string ct)
{
this._contentType = ct;
}
public override void OnActionExecuted(ActionExecutedContext context) { /* nada */ }
public override void OnActionExecuting(ActionExecutingContext context)
{
context.HttpContext.Response.ContentType = this._contentType;
}
} [ContentType("text/css")]
public ActionResult Style()
{
var model = new Style
{
Color = "red",
Background = "blue"
};
return View(model);
} public class JavaScriptSettingsController : Controller
{
public ActionResult Index()
{
return PartialView();
}
} $(function(){
$.post('@Url.Action("GetData", "Home")', function (data) {
$('.notificationList').html(data);
if ($(data).filter("li").length != 0) {
$('#notificationCounter').html($(data).filter("li").length);
}
});
}); <script src="/JavaScriptSettings"></script>
<script>
$(function () {
$.post('@Url.Action("Index", "Home")', function (data) {
$('.notificationList').html(data);
if ($(data).filter("li").length != 0) {
$('#notificationCounter').html($(data).filter("li").length);
}
});
});
</script> public class ExternalFileAttribute : ActionFilterAttribute
{
private readonly string _contentType;
private readonly string _tag;
public ExternalFileAttribute(string ct, string tag)
{
this._contentType = ct;
_tag = tag;
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var response = filterContext.HttpContext.Response;
response.Filter = new StripEnclosingTagsFilter(response.Filter, _tag);
response.ContentType = _contentType;
}
private class StripEnclosingTagsFilter : MemoryStream
{
private static Regex _leadingOpeningScriptTag;
private static Regex _trailingClosingScriptTag;
//private static string Tag;
private readonly StringBuilder _output;
private readonly Stream _responseStream;
/*static StripEnclosingTagsFilter()
{
LeadingOpeningScriptTag = new Regex(string.Format(@"^\s*<{0}[^>]*>", Tag), RegexOptions.Compiled);
TrailingClosingScriptTag = new Regex(string.Format(@"</{0}>\s*$", Tag), RegexOptions.Compiled);
}*/
public StripEnclosingTagsFilter(Stream responseStream, string tag)
{
_leadingOpeningScriptTag = new Regex(string.Format(@"^\s*<{0}[^>]*>", tag), RegexOptions.Compiled);
_trailingClosingScriptTag = new Regex(string.Format(@"</{0}>\s*$", tag), RegexOptions.Compiled);
_responseStream = responseStream;
_output = new StringBuilder();
}
public override void Write(byte[] buffer, int offset, int count)
{
string response = GetStringResponse(buffer, offset, count);
_output.Append(response);
}
public override void Flush()
{
string response = _output.ToString();
if (_leadingOpeningScriptTag.IsMatch(response) && _trailingClosingScriptTag.IsMatch(response))
{
response = _leadingOpeningScriptTag.Replace(response, string.Empty);
response = _trailingClosingScriptTag.Replace(response, string.Empty);
}
WriteStringResponse(response);
_output.Clear();
}
private static string GetStringResponse(byte[] buffer, int offset, int count)
{
byte[] responseData = new byte[count];
Buffer.BlockCopy(buffer, offset, responseData, 0, count);
return Encoding.Default.GetString(responseData);
}
private void WriteStringResponse(string response)
{
byte[] outdata = Encoding.Default.GetBytes(response);
_responseStream.Write(outdata, 0, outdata.GetLength(0));
}
}
} [ExternalFile("text/javascript", "script")]
public ActionResult Index()
{
return PartialView();
} [ExternalFile("text/css", "style")]
public ActionResult Style()
{
var model = new Style
{
Color = "red",
Background = "blue"
};
return View(model);
} <div data-url="@Url.Action(....)"> </div>
var url = $("div").data("url") ;