Posts

Showing posts with the label Styles

Add Styles & Scripts Dynamically to head tag in ASP .NET MVC

Hey, I wanted to be able to create partial views in asp .net mvc in a way that every partial will declare its own styles and scripts. So the naive solution is to define a section in the layout, and in the partial view just put everything inside. The problem is, it's working only for one level deep. What happens if your partial view uses another partial views ? What happens if your layout is dynamic, and uses partial views as well? So I created the following solution: public class DynamicHeaderHandler : ActionFilterAttribute { private static readonly byte[] _headClosingTagBuffer = Encoding.UTF8.GetBytes("</head>"); private static readonly byte[] _placeholderBuffer = Encoding.UTF8.GetBytes(DynamicHeader.SectionPlaceholder); public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.Result is ViewResult) { filterContext.HttpContext.Response.Filter = new ResponseFilter(filterContext...