zoukankan      html  css  js  c++  java
  • 关于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

  • 相关阅读:
    热烈祝贺自己的博客进入前1000名
    烈日之后逛成都
    懂得爱情
    腾飞天涯
    哈佛教授教你成为No.1的秘诀
    烈日炎炎
    有缘无份——因为伤感所以美丽
    时间管理
    亲爱的陌生人(转)
    走好激励第一步
  • 原文地址:https://www.cnblogs.com/shiningrise/p/5848022.html
Copyright © 2011-2022 走看看