gpt4 book ai didi

c# - 限制 HttpModule 只处理某些请求

转载 作者:行者123 更新时间:2023-11-30 16:19:51 25 4
gpt4 key购买 nike

我们在网站的特定部分使用目录浏览,但我们的用户并不真正喜欢默认的 ASP.NET

目录浏览。老实说,我们也不是特别关心它。

我遇到了 mvolo 的自定义目录浏览模块,并尝试使用它。但是,我发现如果我在我的根 web.config 中启用它,它允许在没有默认页面的情况下浏览所有文件夹的目录(正如您所期望的那样)。如果我在根目录中设置 enabled="false",它会抛出一个 HttpException,它会被我的一般错误页面捕获,但每个请求都会导致异常,例如当请求的页面在加载过程中有额外的图像要请求时。

我相信(我可能是错的),如果没有默认文件夹并且您没有请求特定文件(例如,mysite.com/images/与 mysite.com/images/logo.gif 相比)。

我已经重建了自定义模块的功能,但我无法弄清楚如何将模块限制为仅在启用时需要目录浏览的情况下完全执行——而不是针对每个请求。这是模块中的一段代码:

    public void Init(HttpApplication app)
{
app.PreRequestHandlerExecute += new EventHandler(this.OnPreRequestHandlerExecute);
}

public void OnPreRequestHandlerExecute(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
config = (DirectoryListingConfigSection)WebConfigurationManager.GetSection("directoryBrowsing", context.Request.Path);

if (this.config == null)
{
throw new Exception("Missing <directoryBrowsing> configuration section.");
}

/* I only want to check this if it's necessary, not for things
like mysite.com/images/logo.gif or mysite.com/about/history.aspx
-- those shouldn't give a 403 error */
if (!config.Enabled)
{
context.Response.Status = "403 Forbidden";
}

/* The rest of the code goes below, and should only process
if Directory Browsing is necessary and enabled */
}

最佳答案

模块是 executed on every request通过 ASP.Net,无法根据请求类型限制对模块的调用。

您需要在模块代码中构建检查,以仅处理该模块感兴趣的请求。

根据阶段,您应该可以访问有关请求的大部分信息。在 PreRequestHandlerExecute 期间,您拥有有关传入请求的所有可能信息,包括 Url、 header 和相关 session 状态(如果存在)。

关于c# - 限制 HttpModule 只处理某些请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14899806/

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