关于linux asp.net MVC网站中 httpHandlers配置无效的处理方法

近期有Jexus用户反映,在Linux ASP.NET MVC网站的Web.config中添加 httpHandlers 配置用于处理自定义类型,但是在运行中并没有产生预期的效果,服务器返回了404(找不到网页)错误。经我亲自测试,在WebForm网站中,httpHandlers节点的配置是有效的,而在MVC中的确无效。
造成httpHandlers无效的原因我并没有时间去深究,为了能及时解决这个问题,我把注意力放到了Global.asax文件的Application_BeginRequest方法,并给出如下的解决方案:
一,在global.asax中添加一个静态方法:
static bool TryHanler<T>(string ext) where T : IHttpHandler
{
    if (string.IsNullOrEmpty(ext)) return false;
    var context = HttpContext.Current;
    var path = context.Request.AppRelativeCurrentExecutionFilePath;
    if (!path.EndsWith(ext)) return false;
    var handle = Activator.CreateInstance(typeof(T)) as IHttpHandler;
    if (handle == null) return false;
    handle.ProcessRequest(context);
    context.Response.End();
    return true;
}
说明:这是一个泛型方法,T表示你用于处理某个路径的继承自IHttpHandler的处理类,该方法的参数是这个处理类对应的路径的扩展名(含“.”号)。
二,在global.asax中添加Application_BeginRequest方法,并在该方法中调用TryHandler。如:
protected void Application_BeginRequest(object sender, EventArgs e)
{
    if(TryHandler<myHandler>(".do")) return;
}

http://www.linuxdot.net/bbsfile-4310

 

posted @ 2016-09-07 08:21  shiningrise  阅读(298)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css