gpt4 book ai didi

c# - HttpModule 不会在每个请求上都被调用

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:14 26 4
gpt4 key购买 nike

我在服务器上安装了一个 HTTP 模块。奇怪的是它可以工作,但每隔一段时间它就不会被执行。我有日志记录,在它不起作用的时候,它没有到达记录的代码。我在 IIS 日志或事件查看器中也没有看到任何内容。

    namespace RedirectModule
{
public class RedirectModule : System.Web.IHttpModule
{
private const string MobileUserAgent = "MobileUserAgentCacheKey";

private const string

STRING_TO_FIND = "info/lps";
private const string STRING_TO_ADD = "/mobile";


public void Dispose()
{
//clean-up code here.
}

public void Init(HttpApplication context)
{
context.BeginRequest += context_BeginRequest;
}
private static object sync = new object();
private void context_BeginRequest(object sender, EventArgs e)
{

try
{


HttpContext context = HttpContext.Current;


string url = context.Request.Url.ToString().ToLower();

if (!url.Contains(STRING_TO_FIND) || url.Contains(STRING_TO_FIND + STRING_TO_ADD))
return;
Logger.Current.Log("Starting Redirect Phase");

if (XmlToValues.IsMobile(context.Request.ServerVariables["HTTP_USER_AGENT"],
GetCachedFile(context, "Settings.xml")))
{
var mobileRedirect = GetRedirectUrl(url, STRING_TO_FIND, STRING_TO_ADD);
if (mobileRedirect != null)
{
Logger.Current.Log("Redirect to Mobile page");
context.Response.Redirect(mobileRedirect);

}
}
Logger.Current.Log("Web Page");
Logger.Current.Log("End Begin Request");
}
catch (Exception ex)
{
if (ex is ThreadAbortException)
return;

Logger.Current.LogError(ex);

}
}


public static string GetRedirectUrl(string url, string strToFind, string strToAdd)
{
try
{
Logger.Current.Log("Get Redirect Url ");
int idx = url.IndexOf(strToFind) + strToFind.Length;
return url.Substring(0, idx) + strToAdd + url.Substring(idx);
}
catch (Exception ex)
{

Logger.Current.LogError(ex);


return null;
}
}



private XmlNodeList GetCachedFile(HttpContext context, string filePath)
{
try
{
Logger.Current.Log("GetCachedFile START");
if (context.Cache[MobileUserAgent] == null)
{
context.Cache[MobileUserAgent] = XmlToValues.GetMobileUserAgents(filePath);
Logger.Current.Log("Add Mobile File to Cache");
}
return (XmlNodeList)context.Cache[MobileUserAgent];
}
catch (Exception ex)
{
Logger.Current.LogError(ex);

return null;
}
}

}
}

和我的 Web.Config:

<?xml version="1.0" encoding="UTF-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>


<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="RedirectModule" />
<add name="RedirectModule" type="RedirectModule.RedirectModule, RedirectModule" />


</modules>
<handlers>
<remove name="Redirect" />
</handlers>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
<system.web>
<httpModules>
<add name="RedirectModule" type="RedirectModule.RedirectModule, RedirectModule" />
</httpModules>
<compilation debug="true">
</compilation>
</system.web>
</configuration>

附注web.config里的log4net去掉了,嫌麻烦。

这是项目的链接:http://www.sendspace.com/file/w42me5

这是被请求页面的标记,它在一个名为 index.htmnl 的文件中:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<!-- no cache headers -->
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="no-cache">
<meta http-equiv="Expires" content="-1">
<meta http-equiv="Cache-Control" content="no-cache">
<!-- end no cache headers -->
</head>
<body>
MOBILE
</body>
</html>

最佳答案

我有一个类似的问题...尝试在您的网络浏览器中关闭缓存并重试。为了关闭此请求的缓存,您需要修改响应 header 。 Example on modifying caching option

关于c# - HttpModule 不会在每个请求上都被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11756038/

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