星空网 > 软件开发 > ASP.net

关于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。

 




原标题:关于Linux ASP.NET MVC网站中 httpHandlers配置无效的解决方案

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亿龙达供应链:https://www.goluckyvip.com/tag/47535.html
亿品贸易:https://www.goluckyvip.com/tag/47536.html
义博会:https://www.goluckyvip.com/tag/47537.html
义达国际物流:https://www.goluckyvip.com/tag/47538.html
义达物流:https://www.goluckyvip.com/tag/47539.html
关键词定位:https://www.goluckyvip.com/tag/4754.html
美众议院对TikTok“动手”,下一步该怎么在TikTok上营销?:https://www.kjdsnews.com/a/1836587.html
速卖通在韩国争议不断,投诉量激增两倍:https://www.kjdsnews.com/a/1836588.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流