2013-04-30 16 views
3

我在MVC中沒有使用過httpHandlers。 但是我想在應用程序中停止會話超時。 我在這裏找到了解決方案; http://www.dotnetcurry.com/ShowArticle.aspx?ID=453如何在MVC中通過JQuery調用HttpHandler

但是我的執行力度,我得到錯誤信息

的路徑控制器「/Shared/KeepSessionAlive.ashx」未找到 ,或者不執行一個IController

所以jQuery的;

$.post("/Shared/KeepSessionAlive.ashx", null, function() { 
    $("#result").append("<p>Session is alive and kicking!<p/>"); 
}); 

正在尋找一個控制器。我該如何停止並執行處理程序代碼?

我試着把這個放在我的web.config中;

<httpHandlers> 
    <add verb="*" path="KeepSessionAlive.ashx" type="XXXXXX.Views.Shared.KeepSessionAlive"/> 
</httpHandlers> 

回答

3

嘗試忽略.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路由忽略.ashx的文件/共享/;但是,它不適用於其他地方的.ashx文件。如果你想讓它在所有的子目錄中都能正常工作,請嘗試下面的代碼(用this answer代替這個技巧):

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