我正在尝试找到一种方法来以编程方式检查是否加载了特定的HttpModule(作为我正在编写的组件需要模块正常工作).我尝试着:
bool ismodulepresent = false;
foreach(HttpModuleAction module in ((HttpModulesSection)ConfigurationManager.GetSection("system.web/httpModules")).Modules)
{
if(module.Type == typeof(MyModule).FullName)
{
ismodulepresent = true;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
但这仅适用于IIS5.1 <httpModules>部分,而不适用于较新的<system.webServer>部分.
有没有想过除了检查两个部分之外还有更好的方法吗?
我想捕获ASP.NET MVC应用程序中的请求的命中时间,处理时间,内存消耗和响应时间.
有没有办法或工具来执行此操作?
我遇到了一个特别有趣的情况.我有一个通用的错误处理例程当前工作.最近我注意到了一个有点奇怪的行为:HttpApplication.Error会启动,但HttpContext.Current会为空.这是我的HttpModule上的相关内容:
public void Init(HttpApplication context)
{
context.Error += context_Error;
context.PostMapRequestHandler += context_PostMapRequestHandler;
}
void context_PostMapRequestHandler(object sender, EventArgs e)
{
var aux = HttpContext.Current.Handler as Page;
if (aux != null) aux.Error += context_Error;
}
void context_Error(object sender, EventArgs e)
{
_localLog.Add("HttpApplication error handler reached.");
try
{
if (HttpContext.Current == null)
{
_localLog.Add("No HttpContext.");
}
else
{
var objError = HttpContext.Current.Server.GetLastError();
if (objError == null)
{
_localLog.Add("GetLastError(): no error.");
return;
}
//[Proper error handler follows here...]
}
} …Run Code Online (Sandbox Code Playgroud) 我已经在那里我使用的无"www"的网址(重定向模块做了一个Web应用程序http://example.com/)与"WWW"的网址(http://www.example.com/).但正如我共享托管服务器,在那里我没有实现的HttpModule权限,然后我试图与Global.asax文件相同的模块代码.这样可行!
我使用以下(Application_BeginRequest())事件来实现我的HttpModule功能.
void Application_BeginRequest()
{
//module code
}
Run Code Online (Sandbox Code Playgroud)
从Global.asax文件中模块和应用程序运行良好但是我担心性能.
为什么我们在asp.net中使用HTTPModules如果我们可以使用Global.asax文件实现相同的.两者之间是否存在性能差异.或者在使用Global.asax文件而不是HttpModule时需要担心的任何差异?
请解释!
我正在尝试访问HttpModule中的一个页面,我想我应该通过调用HttpContext.Current.Handler(这应该引用当前页面)来做到这一点,但我一直都是null.
我正在开发使用.Net 3.5框架.
我在AuthorizeRequest和AuthenticateRequest上检查这个
谢谢.
我是否必须锁定对实例成员的访问权限?
例:
public class HttpModule : IHttpModule
{
//...
Dictionary<int, int> foo;
void UseFoo(int a, int b)
{
foo[a] = b;
}
}
Run Code Online (Sandbox Code Playgroud) 我们正在为ASP.NET应用程序使用进程外会话提供程序(ScaleOut),我们注意到当一个没有正确设置反序列化的对象无意中进入会话时,它最终会导致整个终止的过程.
重现和处理这种情况是它变得更有趣的地方.
在AnyStaObjectsInSessionState中引发了终止进程的异常,其实现非常简单:
internal static bool AnyStaObjectsInSessionState(HttpSessionState session)
{
if (session != null)
{
int count = session.Count;
for (int i = 0; i < count; i++)
{
object obj2 = session[i];
if (((obj2 != null) && (obj2.GetType().FullName == "System.__ComObject"))
&& (UnsafeNativeMethods.AspCompatIsApartmentComponent(obj2) != 0))
{
return true;
}
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
这是堆栈跟踪,显示了异常如何终止进程:
An unhandled exception occurred and the process was terminated.
Application ID: /LM/W3SVC/1/ROOT
Process ID: 4208
Exception: System.Runtime.Serialization.SerializationException
Message: …Run Code Online (Sandbox Code Playgroud) 最近,我的团队被要求为ASP.NET MVC应用程序实现一个HttpModule,该应用程序处理IIS 7和.NET 3.5上的双重编码URL.这是问题的症结所在:
我们有时会得到具有双重编码正斜杠的URL,如下所示:
http://www.example.com/%252fbar%5cbaz/foo
Run Code Online (Sandbox Code Playgroud)
还有其他格式我们必须处理,但它们都有一些共同点,它们有一个双重编码的正斜杠.
为了解决这个问题,我们编写了一个HttpModule,它仅在URL具有双重编码的正斜杠时起作用,并且我们将其重定向到一个合理的URL.细节并不重要,但有两个位:
这是问题所在:
IIS启动后的第一个请求显示的URL与第二个请求不同.
如果我们使用上面示例中的URL,则对IIS的第一个请求如下所示:
http://www.example.com/bar/baz/foo
第二个请求看起来像:
http://www.example.com/%252fbar%5cbaz/foo
这是通过Application.Request.Url.AbsolutePath在调试时检查属性来完成的.
这是应该重现问题的最小代码示例(创建一个新的MVC应用程序,并注册以下HttpModule):
public class ForwardSlashHttpModule : IHttpModule
{
internal IHttpApplication Application { get; set; }
public void Dispose()
{
Application = null;
}
public void Init(HttpApplication context)
{
Initialize(new HttpApplicationAdapter(context));
}
internal void Initialize(IHttpApplication context)
{
Application = context;
context.BeginRequest += context_BeginRequest;
}
internal void context_BeginRequest(object sender, EventArgs e)
{
var url = Application.Request.Url.AbsolutePath; //<-- Problem point
//Do stuff with Url …Run Code Online (Sandbox Code Playgroud) 是否可以在HTTP模块中实现OAuth(开放授权)2.0或1.0。
为什么要选择这个,因为每个请求都将首先到达HTTP模块,因此我必须从该请求中进行身份验证
可能吗 ?
如果是,请为此提供一个相关链接
帮我..
在 asp.net 4.0 中,我们可以使用 http 模块来重写模块,如下所示:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string CountryCodeInUrl = "", redirectUrl="";
var countryCode = CookieSettings.ReadCookie();
if (countryCode=="")
{
countryCode = "gb";
}
if (HttpContext.Current.Request.RawUrl.Length >= 2)
{
CountryCodeInUrl = HttpContext.Current.Request.RawUrl.Substring(1, 2);
}
if (countryCode != CountryCodeInUrl)
{
if (HttpContext.Current.Request.RawUrl.Length >= 2)
{
if (HttpContext.Current.Request.RawUrl.Substring(1, 2) != "")
{
countryCode = HttpContext.Current.Request.RawUrl.Substring(1, 2);
}
}
if(!System.Web.HttpContext.Current.Request.RawUrl.Contains(countryCode))
{
redirectUrl = string.Format("/{0}{1}", countryCode, System.Web.HttpContext.Current.Request.RawUrl);
}
else
{
redirectUrl = System.Web.HttpContext.Current.Request.RawUrl;
}
CookieSettings.SaveCookie(countryCode);
System.Web.HttpContext.Current.Response.RedirectPermanent(redirectUrl);
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用 …
httpmodule ×10
asp.net ×5
c# ×5
iis ×3
.net ×2
asp.net-mvc ×2
asp.net-2.0 ×1
global-asax ×1
handler ×1
ihttpmodule ×1
iis-7 ×1
oauth-2.0 ×1
performance ×1
restart ×1
session ×1