如何在MVC中通过JQuery调用HttpHandler

我之前没有在MVC中使用过httpHandlers。 但是我想在我的应用程序中停止会话超时。 我在这里找到了解决方案; http://www.dotnetcurry.com/ShowArticle.aspx?ID=453

但是,在我的暗示下,我收到了错误消息

路径’/Shared/KeepSessionAlive.ashx’的控制器未找到或未实现IController

所以jquery;

$.post("/Shared/KeepSessionAlive.ashx", null, function () { $("#result").append("

Session is alive and kicking!

"); });

正在寻找一个控制器。 如何停止此操作并执行处理程序代码?

我试过把它放在我的web.config中;

    

尝试忽略路由中的.ashx文件,因此MVC不会尝试将其路由到控制器操作:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("Shared/{resource}.ashx/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } 

这将导致MVC路由忽略/ shared /中的.ashx文件; 但是,它不适用于其他地方的.ashx文件。 如果您希望它在所有子目录中工作,请尝试以下代码(对于此技巧,请归功于此答案 ):

 routes.IgnoreRoute("{*allashx}", new { allashx = @".*\.ashx(/.*)?" });