ASP.NET MVC + IHttpModule =未找到

时间:2011-04-09 19:54:21

标签: asp.net-mvc ihttpmodule

啊解决这个问题,我的思绪非常忙碌。当我注册我的httpmodule时,我得到not-found错误,否则一切都像魅力一样。

这是我的httpmodule

public class UrlNormalizerModule : HttpModuleBase {
    protected override void OnBeginRequest(HttpContextBase context) {
        var originUrl = HttpContext.Current.Request.Url.ToString();
        var normalizedUrl = originUrl.NormalizeUrl(false);
        if (string.Compare(originUrl, normalizedUrl) != 0) {
            var response = context.Response;

            response.StatusCode = (int) HttpStatusCode.MovedPermanently;
            response.Status = "301 Moved Permanently";
            response.RedirectLocation = normalizedUrl;
            response.SuppressContent = true;
            response.End();
        }
    }
}

如何在Web.config中注册模块

<system.webServer>        
    <modules runAllManagedModulesForAllRequests="true">
        <remove name="UrlNormalizerModule" />
        <add name="UrlNormalizerModule" type="MagicByte.Web.Modules.UrlNormalizerModule, MagicByte.Web" />
    </modules>
</system.webServer>

更新{暂时解决问题}

嗯...我刚刚处理了HttpApplication的所有事件,如下面的

context.AuthenticateRequest +=
            (sender, e) => OnAuthenticateRequest(new HttpContextWrapper(((HttpApplication) sender).Context));

我不知道为什么但是当我只处理少量重要事件BeginRequest时,问题就解决了。那么真正的问题是什么呢?

1 个答案:

答案 0 :(得分:0)

您可以尝试从路由引擎中排除用于访问此模块的URL:

routes.IgnoreRoute("UrlNormalizerModule.ashx");