asp教程.net ihttpmodule模板使用者登陸代碼執行個體
void application_acquirerequeststate(object source, eventargs e)
{
httpapplication application = (httpapplication)source;
user user = (user)application.context.session["user"]; //擷取user
if (user == null)
{
// application.context.server.transfer("webform1.aspx");
string requesturl = application.request.url.tostring();
string requestpage = requesturl.substring(requesturl.lastindexof('/') + 1);
// 如果請求的頁面不是登入頁面,剛重新導向到登入頁面。
if (requestpage != "login.aspx")
// application.server.transfer("ihttphandlertext/login/login.aspx");
application.response.redirect("login/login.aspx");
}
else
{
application.response.write(string.format("歡迎您!{0}!", user.name));
}
}
%>
建立自訂 http 模組
ihttpmodule 介面
向實作類別提供模組初始化和處置事件。
命名空間:system.web
程式集:system.web(在 system.web.dll 中)
文法
public interface ihttpmodule
public interface ihttpmodule
public class helloworldmodule : ihttpmodule
{
public helloworldmodule()
{
}
public string modulename
{
get { return "helloworldmodule"; }
}
// in the init function, register for httpapplication
// events by adding your handlers.
public void init(httpapplication application)
{
application.beginrequest +=
(new eventhandler(this.application_beginrequest));
application.endrequest +=
(new eventhandler(this.application_endrequest));
}
private void application_beginrequest(object source,
eventargs e)
{
// create httpapplication and httpcontext objects to access
// request and response properties.
httpapplication application = (httpapplication)source;
httpcontext context = application.context;
context.response.write("<h1><font color=red>
helloworldmodule: beginning of request
</font></h1><hr>");
}
private void application_endrequest(object source, eventargs e)
{
httpapplication application = (httpapplication)source;
httpcontext context = application.context;
context.response.write("<hr><h1><font color=red>
helloworldmodule: end of request</font></h1>");
}
public void dispose()
{
}
}