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

Posted 吴晓阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于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

 

以上是关于关于linux asp.net MVC网站中 httpHandlers配置无效的处理方法的主要内容,如果未能解决你的问题,请参考以下文章