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

  • 相关阅读:
    Java-数据字符串进行四舍五入
    Git本地安装及汉化
    Navicat安装教程
    Jdk+maven安装
    系列文章
    @PathVariable
    feign组件
    Lombok
    常见Jar包的用途
    iOS archive(归档)的总结
  • 原文地址:https://www.cnblogs.com/shiningrise/p/5848022.html
Copyright © 2011-2022 走看看