开发者

How can I remove white-spaces from ASP.NET MVC# output?

How can I remove all white-spaces from ASP.NET MVC 3 output?


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开发者_JAVA技巧 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);

  }

Then we should make some changes in web.config file that located in Views folder -see here for more details.

    <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>


one way you can, is that create a custom view page's inheritance; in that, override Write() methods (3 methods will be founded), and in these methods, cast objects to strings, remove white-spaces, and finally call the base.Write();


You could use the String.Replace method:

string input = "This is  text with   ";
string result = input.Replace(" ", "");

or use a Regex if you want to remove also tabs and new lines:

string input = "This is   text with   far  too   much \t  " + Environment.NewLine +
               "whitespace.";
string result = Regex.Replace(input, "\\s+", "");


 Str = Str.Replace(" ", "");

should do the trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜