asp.net-mvc – 缩小ASP.NET MVC中的Action Filter属性
发布时间:2021-01-24 06:02:41 所属栏目:asp.Net 来源:互联网
导读:我有一个控制器动作,返回大量的动态 JavaScript(一次服务到客户端),我已经启用了GZip压缩.我想做的一件事是阅读执行的结果流并对其应用JS缩小. 是否可以使用动作过滤器属性执行此操作.我想我的问题归结为 – 假设我的minifier需要一串JavaScript,有没有办法将
|
我有一个控制器动作,返回大量的动态 JavaScript(一次服务到客户端),我已经启用了GZip压缩.我想做的一件事是阅读执行的结果流并对其应用JS缩小. 是否可以使用动作过滤器属性执行此操作.我想我的问题归结为 – 假设我的minifier需要一串JavaScript,有没有办法将执行结果拉出View(view).ExecuteResult(ControllerContext)? 解决方法我认为YUI Compressor for .NET将完全满足您的需求.http://yuicompressor.codeplex.com/ 编辑:上面错了,因为我误解了这个问题.下面的代码将安装一个响应过滤器,允许您操作输出,在这种情况下,它只是删除换行符. 希望这可以帮助. [HandleError]
public class HomeController : Controller
{
[Minify]
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
}
public class Minify : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//You can check if the content type is CSS/JS here and prevent the filter running on HTML pages
filterContext.HttpContext.Response.Filter = new MinifyFilter(filterContext.HttpContext.Response.Filter);
base.OnActionExecuting(filterContext);
}
}
public class MinifyFilter : MemoryStream
{
private StringBuilder outputString = new StringBuilder();
private Stream outputStream = null;
public MinifyFilter(Stream outputStream)
{
this.outputStream = outputStream;
}
public override void Write(byte[] buffer,int offset,int count)
{
outputString.Append(Encoding.UTF8.GetString(buffer));
}
public override void Close()
{
//Call the minifier here,your data is in outputString
string result = outputString.ToString().Replace(Environment.NewLine,string.Empty);
byte[] rawResult = Encoding.UTF8.GetBytes(result);
outputStream.Write(rawResult,rawResult.Length);
base.Close();
outputStream.Close();
}
} (编辑:日照站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何处理在MVC视图中应用程序启动和传输和显示错误发生的AS
- asp.net-mvc – Nhibernate / MVC:在View中处理延迟加载的
- asp.net-mvc – Url.Action生成查询字符串,以任何方式生成完
- asp.net – 如何删除日历的最后一周
- asp.net-mvc-4 – WepApi控制器是否应该返回viewmodels
- asp.net – 如何从日历控件中获取所选日期?
- asp.net-mvc – 如何在asp.net mvc中处理分页?
- 遭遇Asp.Net长文件名下载的问题和解决办法
- azure – 当用户存储在外部身份提供程序服务中时与用户的关
- asp.net-mvc – ASP.NET MVC推荐的依赖注入框架是什么?
推荐文章
站长推荐
- asp.net-mvc-4 – MVC4捆绑GZIP和头文件
- asp.net文件上传解决方案(图片上传、单文件上传
- asp.net-mvc – 使用没有ORM的ASP.NET MVC
- asp.net – Intranet / Internet的Windows身份验
- asp.net-mvc – ASP.NET MVC 3 Treeview
- asp.net-mvc – 在没有模型的情况下手动将验证添
- SqlServer如何给表添加新的字段以及字段注释
- asp.net-mvc – 当我不知道内容类型时如何返回文
- asp.net-core – 构建asp.net核心错误
- asp.net-mvc – TempData在第二个请求后不会被破
热点阅读
