gpt4 book ai didi

c# - 处理对 asp.net 空 web 应用程序的所有请求

转载 作者:行者123 更新时间:2023-11-30 21:37:42 24 4
gpt4 key购买 nike

我想处理所有请求。 我无法处理具有扩展名的请求。让我告诉你我的意思。

1) 首先让我向您展示我的网络应用程序有什么:(非常简单)

Web.config

<?xml version="1.0"?>
<configuration>

<system.web>
<customErrors mode="On" defaultRedirect="Handle404.aspx">
<error statusCode="404" redirect="Handle404.aspx" />
</customErrors>
<compilation debug="true"/>
</system.web>

</configuration>

Global.asax

using System;

namespace DeleteMeWebAppliation
{
public class Global : System.Web.HttpApplication
{
protected void Application_BeginRequest(object sender, EventArgs e)
{
Console.Write("Test");
}

protected void Application_Error(object sender, EventArgs e)
{
Console.Write("Test");
}
}
}

Default.aspx

using System;

namespace DeleteMeWebAppliation
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Console.WriteLine("test");
}
}
}

Handle404.aspx

using System;

namespace DeleteMeWebAppliation
{
public partial class Handle404 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Console.Write("Test");
}
}
}

2) 问题来了:

请注意,我在所有行 Console.Write("Test") 上都有一个断点,我的目标是处理所有请求。 **换句话说,无论 url 是什么,我都想在每次向 IIS 发出请求时命中断点。

a) 当我转到 http://localhost:65284/ It Works (我遇到了一个断点)

b) 当我转到 http://localhost:65284/SomeNoneExistingPage.aspx It Works (我遇到了一个断点)

c) 当我转到 http://localhost:65284/Foooooo It Works (我遇到了一个断点)

d) 当我转到 http://localhost:65284/Foooooo/fooo/fooo/test.aspx 有效(我遇到了断点)

e) 当我转到 http://localhost:65284/foo.test 失败(我没有遇到断点) 为什么断点在方法上Application_Error 没有命中!?

e) 当我访问 http://localhost:65284/foo/foo.jpg 失败(我没有遇到断点) 为什么断点方法 Application_Error 没有命中!?

总而言之,当有人访问 http://localhost:65284/foo.test 时,我如何执行我的自定义代码。一切都很好,但当我转到一个带有“。”的网址时最后用别的东西而不是 aspx 然后它失败了!

互联网上有很多地方展示了如何处理 404 错误,但由于某些原因我无法使它起作用。


这是我在访问 http://localhost:65284/foo.test 时得到的结果 我收到 404 错误并且由于某种原因我没有被重定向到 404 处理程序。如果链接以 aspx 结尾,我将被重定向。没意义

enter image description here

最佳答案

找到解决方案。

Web 配置看起来像:

<?xml version="1.0"?>
<configuration>

<system.web>
<httpHandlers>
<add verb="*" path="*" type="HandleAllRequestsHandler"/>
</httpHandlers>
<compilation debug="true"/>
</system.web>
<system.webServer>
<handlers>
<add verb="*" path="*" name="ImageWatermarkHandler" type="HandleAllRequestsHandler"/>
</handlers>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>

</configuration>

现在我用这个处理程序处理所有请求:

using System;
using System.Web;

public class HandleAllRequestsHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Console.WriteLine("Test!");
}

public bool IsReusable => false;
}

请注意,我在这里处理所有请求,因为动词和路径的值为 *。将您的自定义过滤器放在那里

关于c# - 处理对 asp.net 空 web 应用程序的所有请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984721/

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