我有一个安装在我们服务器上的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>
P.S。我在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>
答案 0 :(得分:0)
我有一个类似的问题...尝试在你的webbrowser中转换缓存并再试一次。要关闭此请求的缓存,您需要修改响应头。 Example on modifying caching option
答案 1 :(得分:0)
听起来你可能正在浏览器和服务器之间的某个地方点击缓存。缓存有很多潜在的地方,这里有一些尝试找到它的一般步骤:
要防止缓存,您可以向请求添加无缓存标头(文章here):
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays( -100 );
context.Response.AddHeader( “pragma”, “no-cache” );
context.Response.AddHeader( “cache-control”, “private” );
context.Response.CacheControl = “no-cache”;
}
修改的
响应中的HTTP 200表明您很可能没有缓存问题。要确认每个请求实际上得到到服务器,尝试添加日志到您的Application_BeginRequest处理程序(在Global.asax中)来记录每一个请求,并把它比作在你context_BeginRequest生成的日志你的HTTP模块。
//In Global.asax
private void Application_BeginRequest(Object source, EventArgs e) {
Logger.Current.Log("Request made to Application_BeginRequest")
}
//In your HttpModule
private void context_BeginRequest(object sender, EventArgs e)
{
//Log before any of your other code
Logger.Current.Log("Request made to context_BeginRequest")
try
{
// your code
}
catch (Exception ex)
{
// log the exception first in case this is the problem
Logger.Current.LogError(ex);
if (ex is ThreadAbortException)
return;
}
}
在没有到达您的模块的请求后检查您的日志。