1

我是 ASP.NET 的新手,我已经在 Google 上搜索过这个我宁愿不做的问题,因为对于 ASP.NET 来说是新手,只需从 Google 复制和粘贴即可获得零知识。

我正在使用一本向我展示如何做到这一点的特定书籍,但是它不起作用。图书网站上没有勘误表,所以我认为这些问题可能与我的实施有关。

我需要从我的页面中减少超过换行符的空白。我不希望所有代码都网格在一起,但至少在代码组之间有一个空间。

我创建了一个 HTTP Modul Module.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace authentication2
{
public class Module: IHttpModule
{
public void Init(HttpApplication httpApplication)
{
    httpApplication.PostRequestHandlerExecute +=
        new EventHandler(OnPostRequestHandlerExecute);
}

private void OnPostRequestHandlerExecute(Object sender, EventArgs e)
{
    HttpApplication httpApplication = (HttpApplication)sender;
    HttpResponse httpResponse = httpApplication.Context.Response;

    if (httpResponse.ContentType == "text/html")
    {
        httpResponse.Filter = 
            new WhiteSpaceFilterStream(httpResponse.Filter);
    }
}

public void Dispose()
{
}
}
}

我创建了一个过滤器类:WhiteSpaceFilterStream.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace authentication2
{
class WhiteSpaceFilterStream : Stream
{
    private Stream outputStream = null;

    public WhiteSpaceFilterStream(Stream outputStream)
    {
        this.outputStream = outputStream;
    }

    private bool inWhiteSpaceRun = false;
    private bool whiteSpaceRunContainsLineBreak = false;

    public override void Write(byte[] buffer, int offset, int count)
    {
        int dataLimit = offset + count;
        for (int i = offset; i < dataLimit; i++)
        {
            char c = (char)buffer[i];

            if (Char.IsWhiteSpace(c))
            {
                inWhiteSpaceRun = true;

                if ((c == '\r') || (c == '\n'))
                {
                    whiteSpaceRunContainsLineBreak = true;
                }
            }
            else
            {
                // If this is the end of a white space run, write a single
                // white space or line break
                if (inWhiteSpaceRun)
                {
                    // If the white space run contains a line break,
                    // write a line break, so we don't break JavaScript.
                    // Otherwise just write a space.
                    if (whiteSpaceRunContainsLineBreak)
                    {
                        outputStream.WriteByte((byte)'\r');
                        outputStream.WriteByte((byte)'\n');
                    }
                    else
                    {
                        outputStream.WriteByte((byte)' ');
                    }
                }

                outputStream.WriteByte((byte)c);

                inWhiteSpaceRun = false;
                whiteSpaceRunContainsLineBreak = false;
            }
        }
    }

    public override bool CanWrite
    {
        get { return true; }
    }

    public override bool CanSeek
    {
        get { return false; }
    }

    public override bool CanRead
    {
        get { return false; }
    }

    public override long Length
    {
        get { throw new InvalidOperationException(); }
    }

    public override long Position
    {
        get { throw new InvalidOperationException(); }
        set { throw new InvalidOperationException(); }
    }

    public override void Flush()
    {
        outputStream.Flush();
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        throw new NotImplementedException();
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        throw new NotImplementedException();
    }

    public override void SetLength(long value)
    {
        throw new NotImplementedException();
    }

    public override void Close()
    {
        outputStream.Flush();
        outputStream.Close();
        base.Close();
    }
}
}

最后,我将其添加到 Web 配置文件中:

<system.web>
<httpModules>
<add name="WhiteSpaceFilter" type="authentication2.Module, WhiteSpaceFilter"/>
</httpModules>
</system.web>

我玩过配置文件,但不确定它是否正确:authentication2是项目的名称。

我没有收到任何错误,但是,空白并没有减少。

我看到很多例子,但大多数都提到 MVC,我不太了解。

我被困住了!

谢谢你的时间。

4

0 回答 0