如何从ASP.NET MVC#输出中删除的空格? [英] How can I remove white-spaces from ASP.NET MVC# output?
问题描述
我如何从ASP.NET MVC 3输出中删除所有的空格?
How can I remove all white-spaces from ASP.NET MVC 3 output?
更新:我知道我可以使用与string.replace方法或普通防爆pressions从一个字符串中删除的空格;但我不知道,我该如何使用主题ASP.NET MVC 3 remove从输出字符串中的所有的空格。例如,当OnResultExecuted的方法调用,其结果是准备发送给最终用户的浏览器,我想获得的结果 - 作为一个字符串或Stream对象;它们之间的区别不 - ,做就可以了我的工作。
谢谢大家。 :)
UPDATE: I know how can I use string.Replace method or Regular Expressions to remove white-spaces from a string; But I don't know, how can I use theme in ASP.NET MVC 3 for remove all white-spaces from output-string. For example, when the "OnResultExecuted" method invoked, and the result is ready to send to the end-user's browser, I want to obtain result - as a String, or a Stream object; not difference between them -, and do my job on it. Thanks to all. :)
推荐答案
我发现我的答案,创建这样一个最终的解决方案:
I found my answer and create a final solution like this:
首先创建一个基类,迫使意见,从继承,如下图所示,并覆盖一些方法:
First create a base-class to force views to inherit from that, like below, and override some methods:
public abstract class KavandViewPage < TModel > : System.Web.Mvc.WebViewPage < TModel > {
public override void Write(object value) {
if (value != null) {
var html = value.ToString();
html = REGEX_TAGS.Replace(html, "> <");
html = REGEX_ALL.Replace(html, " ");
if (value is MvcHtmlString) value = new MvcHtmlString(html);
else value = html;
}
base.Write(value);
}
public override void WriteLiteral(object value) {
if (value != null) {
var html = value.ToString();
html = REGEX_TAGS.Replace(html, "> <");
html = REGEX_ALL.Replace(html, " ");
if (value is MvcHtmlString) value = new MvcHtmlString(html);
else value = html;
}
base.WriteLiteral(value);
}
private static readonly Regex REGEX_TAGS = new Regex(@">\s+<", RegexOptions.Compiled);
private static readonly Regex REGEX_ALL = new Regex(@"\s+|\t\s+|\n\s+|\r\s+", RegexOptions.Compiled);
}
那么我们应该在一些修改的 的web.config
的是位于 查看$文件C $ C>
的文件夹 - <一个href=\"http://stackoverflow.com/questions/3239006/how-to-import-a-namespace-in-razor-view-page/6723046#6723046\">see此处了解更多详细信息。
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="Kavand.Web.Mvc.KavandViewPage"> <!-- pay attention to here -->
<namespaces>
<add namespace="System.Web.Mvc" />
....
</namespaces>
</pages>
</system.web.webPages.razor>
这篇关于如何从ASP.NET MVC#输出中删除的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!