zoukankan      html  css  js  c++  java
  • 针对Linux ASP.NET MVC网站中 httpHandlers配置无效的解决方案

    近期有Linux ASP.NET用户反映,在MVC网站的Web.config中添加 httpHandlers 配置用于处理自定义类型,但是在运行中并没有产生预期的效果,服务器返回了404(找不到网页)错误。经我亲自测试,在WebForm网站中,httpHandlers节点的配置是有效的,而在MVC中的确无效。如果这个问题不能解决,将严重影响Linux ASP.NET的部署,也影响WIN ASP.NET向Linux迁移的兼容性和完整性。

    造成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的自定义类,参数ext是这个处理类所处理的请求路径的扩展名(含“.”号)。

    二,在global.asax中实现Application_BeginRequest方法,并在该方法中调用TryHandler。如:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if(TryHandler<myHandler>(".do")) return;
    }

    注:该处理方案具有通用性,能同时兼容 Windows IIS和 Linux Jexus或XSP。

  • 相关阅读:
    最长回文 hdu3068(神代码)
    1297. Palindrome ural1297(后缀数组)
    705. New Distinct Substrings spoj(后缀数组求所有不同子串)
    Milk Patterns poj3261(后缀数组)
    Musical Theme poj1743(后缀数组)
    Conscription poj3723(最大生成树)
    Drying poj3104(二分)
    Finding LCM (最小公倍数)
    002 全局配置信息
    001 开始
  • 原文地址:https://www.cnblogs.com/yunei/p/5316462.html
Copyright © 2011-2022 走看看