gpt4 book ai didi

c# - 在 Asp.Net 4.0 中部署 HttpModule 时出现问题

转载 作者:行者123 更新时间:2023-12-03 21:52:25 26 4
gpt4 key购买 nike

我想做 UrlRewriting。为此,我编写了一个 HttpModule,它在 ASP.NET Http 模块链中的 AuthorizeRequest 事件上运行(在 HttpHandler 处理请求之前)。

上下文

我写了一个抽象类作为实现 IHttpModule 接口(interface)的基本重写器:

public abstract class BaseModuleRewriter : IHttpModule {
public virtual void Init(HttpApplication app) {
// WARNING! This does not work with Windows authentication!
app.AuthorizeRequest += new ventHandler(this.BaseModuleRewriter_AuthorizeRequest);
}
public virtual void Dispose() { }
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) {
HttpApplication app = (HttpApplication)sender;
this.Rewrite(app.Request.Path, app);
}
protected abstract void Rewrite(string requestedPath, HttpApplication app);
}

模块的真正实现是下面的类:

public class ModuleRewriter : BaseModuleRewriter {
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app) {
// Just dummy, if the module works, I should never get to any page other than homepage
UrlRewritingUtils.RewriteUrl(app.Context, "Default.aspx");
}
}

在我的 web.config 中我有以下内容:

<configuration>
...
<system.web>
<httpModules>
<add type="ModuleRewriter" name="ModuleRewriter"/>
</httpModules>
</system.web>
...
</configuration>

实际上,这是 MSDN Megazine 中发布的一篇文章中 Url-Rewriting 的简单实现。我只是让它变得更简单,但方法就是那个。

问题

没用!当我像我告诉你的那样部署模块并请求页面时,我可以访问我网站的所有页面,相反,我应该总是被“重定向”到 Default.aspx

怎么办?

附加信息

首先,一个重要信息:我的应用程序池是针对框架 4.0 的集成模式

我的网站没有编译。我的意思是我不预编译我的网站,而是将所有代码放在 App_Code 目录中,并让 ASP.NET 在请求页面时对其进行编译。直接后果是我的所有程序集都放在 %sysroot%\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\... 目录中。

我阅读了一些内容,其中通过指定组件在 web.config 中部署了模块,例如:

<configuration>
...
<system.web>
<httpModules>
<add type="ModuleRewriter, ModuleRewriterAssembly" name="ModuleRewriter"/>
</httpModules>
</system.web>
...
</configuration>

但是,由于我的东西不是预编译的,而是完全由 Asp.Net 管理的,所以我不能指定不同的程序集。是否需要解决这个问题?如果是,该怎么办?

谢谢

最佳答案

根据您的 appPool 配置(集成模式与经典模式),尝试添加

  <system.webServer>
<modules>
<add type="ModuleRewriter" name="ModuleRewriter"/>
</modules>
</system.webServer>

关于c# - 在 Asp.Net 4.0 中部署 HttpModule 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com