我想在asp.net中创建一个自定义HTTP模块。 This article显示了如何操作我按照步骤操作,这是继承自IHttpModule
public class LanguageModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.AcquireRequestState += new EventHandler(OnAcquireRequestState);
}
public void Dispose()
{
}
public void OnAcquireRequestState(Object i_object, EventArgs i_eventArgs)
{
HttpApplication l_httpApplication = i_object as HttpApplication;
// check whether the language change parameter has been passed
var l_language =
l_httpApplication.Request.Params[Constants.SESSION_LANGUAGE];
var l_boolLanguageChanged = false;
if (l_language == null)
{
// if language parameter is not sent, then take language from session
l_language = (string)l_httpApplication.Session[Constants.SESSION_LANGUAGE];
}
else
{
// If language parameter is indeed sent, then user wants to change language.
// I will make sure I tag this in order to redirect to.
l_boolLanguageChanged = true;
}
// having the language a thand, let us set it.
var l_culture = new System.Globalization.CultureInfo(l_language);
Thread.CurrentThread.CurrentCulture = l_culture;
Thread.CurrentThread.CurrentUICulture = l_culture;
// save language to session
l_httpApplication.Session[Constants.SESSION_LANGUAGE] = l_language;
// check whether I have redirect
if (l_boolLanguageChanged && l_httpApplication.Request.UrlReferrer != null)
{
l_httpApplication.Response.Redirect(
l_httpApplication.Request.UrlReferrer.AbsolutePath);
}
} // OnAcquireRequestState
//-------------------------
} // class LanguageModule
然后我在web.config
文件中注册了这个模块,如此
<httpModules>
<add name="LanguageSettingModule" type="LanguageModule, App_Code" />
</httpModules>
我还尝试在system.web
标签下注册,如此
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="LanguageSettingModule" type="LanguageModule, App_Code" />
</modules>
</system.webServer>
文件LanguageModule
位于App_Code文件夹
但我总是收到此错误
无法从程序集加载类型&#39; App_Code&#39;。
答案 0 :(得分:1)
从配置设置中删除App_Code
。在web.config
中,它应如下所示。即使您链接的MSDN文档也显示相同的注册方式。
<httpModules>
<add name="LanguageSettingModule" type="LanguageModule" />
</httpModules>