さて、これのために私自身のhttpモジュールを書くことになりました、
public class FileNotFoundModule : IHttpModule
{
private static CustomErrorsSection _configurationSection = null;
private const string RedirectUrlFormat = "{0}?404;{1}";
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.Error += new EventHandler(FileNotFound_Error);
}
private void FileNotFound_Error(object sender, EventArgs e)
{
var context = HttpContext.Current;
if (context != null && context.Error != null)
{
var error = context.Error.GetBaseException() as HttpException;
if (error != null && error.GetHttpCode() == 404 &&
(ConfigurationSecion.Mode == CustomErrorsMode.On || (!context.Request.IsLocal && ConfigurationSecion.Mode == CustomErrorsMode.RemoteOnly)) &&
!string.IsNullOrEmpty(RedirectUrl) && !IsRedirectLoop)
{
context.ClearError();
context.Server.TransferRequest(string.Format(FileNotFoundModule.RedirectUrlFormat, RedirectUrl, context.Request.Url));
}
}
}
private bool IsRedirectLoop
{
get
{
var checkUrl = string.Format(FileNotFoundModule.RedirectUrlFormat,RedirectUrl,string.Empty);
return HttpContext.Current.Request.Url.ToString().Contains(checkUrl);
}
}
private CustomErrorsSection ConfigurationSecion
{
get
{
if (_configurationSection == null)
{
_configurationSection = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath).GetSection("system.web/customErrors");
}
return _configurationSection;
}
}
private string RedirectUrl
{
get
{
foreach (CustomError error in ConfigurationSecion.Errors)
{
if (error.StatusCode == 404)
{
return error.Redirect;
}
}
return string.Empty;
}
}
}
私のために働く:)
/ビクター