2012-07-06 127 views
0

我有一个HttpHandler映射到aspnet_isapi.dll进行静态文件在经典模式下使用IIS 7.5(.pdf文件)的自定义验证检验:呼叫StaticFileHandler

void IHttpHandler.ProcessRequest(HttpContext context) 
{ 
    if(!User.IsMember) { 
    Response.Redirect("~/Login.aspx?m=1"); 
    } 
    else { 
    //serve static content 
    } 
} 

上面的代码工作正常,除else语句逻辑。在else语句中,我只是想允许StaticFileHandler处理请求,但我无法对此进行排序。将不胜感激任何关于如何简单地将文件“交还”回IIS以作为正常的StaticFile请求提供请求的建议。

回答

4

直接回答你的问题,你可以创建一个StaticFileHandler并将其处理请求:

// Serve static content: 
Type type = typeof(HttpApplication).Assembly.GetType("System.Web.StaticFileHandler", true); 
IHttpHandler handler = (IHttpHandler)Activator.CreateInstance(type, true); 
handler.ProcessRequest(context); 

但是一个更好的想法可能是创建一个HTTP模块,而不是HTTP处理程序:

public class AuthenticationModule : IHttpModule 
{ 
    public void Dispose() 
    { 
    } 

    public void Init(HttpApplication application) 
    { 
     application.AuthorizeRequest += this.Application_AuthorizeRequest; 
    } 

    private void Application_AuthorizeRequest(object sender, EventArgs e) 
    { 
     HttpContext context = ((HttpApplication)sender).Context; 
     if (!User.IsMember) 
      context.Response.Redirect("~/Login.aspx?m=1");  
    } 
} 
+0

如果我想以类似的方式提供.aspx页面,而不是调用System.Web.StaticFileHandler类型,我可以使用相同的方法并调用某种类型:System.Web。这种类型是什么? – 2012-08-19 16:17:48

+1

尝试使用'BuildManager.CreateInstanceFromVirtualPath'方法。 – 2012-08-19 20:25:47