私は ASP.NET を初めて使用し、この質問を Google で検索しましたが、ASP.NET を初めて使用するため、Google からコピーして貼り付けるだけでは知識がまったく得られません。
これを行う方法を示した特定の本を使用していますが、機能していません。書籍の Web サイトには正誤表がないため、問題は私の実装に関連している可能性があると考えています。
ページから改行以上の余白を減らす必要があります。すべてのコードを噛み合わせたいわけではありませんが、少なくともコードのグループ間にスペースが必要です。
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 に関するもので、あまり詳しくありません。
ハマった!
お時間をいただきありがとうございます。