无会话的HttpHandler并仅路由到一个控制器.NET的自定义处理程序 [英] Session-less HttpHandler and routing to custom handler for only one controller .NET

查看:39
本文介绍了无会话的HttpHandler并仅路由到一个控制器.NET的自定义处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Web应用程序,它将每30秒对服务器进行AJAX调用,以更新页面中的值.该应用程序本身是MVC和Web窗体的混合体(该应用程序的一部分已升级到MVC,而另一部分仍是旧版).AJAX调用的问题在于,它将重置会话超时,从根本上不会允许用户超时.我发现此问题的解决方法是(ASP.Net MVC中的-request ),该操作将创建一个不使用Session的新处理程序.那应该可以解决问题.

We have an web app that will make an AJAX call to the server every 30 seconds to update a value in a page. The app itself is a mixture of MVC and webforms (part of the app was upgraded to MVC while the other is still legacy). The issue with the AJAX call is that it will reset the session timeout essentially never allowing the user to timeout. A work around I found was from this answer (Disable Session state per-request in ASP.Net MVC) which is to create a new handler that does not use Session. That should solve the issue.

尽管我的第二个问题是,对于使用此无会话"处理程序的唯一控制器,应用程序似乎并没有遵循我的新路由规则:

Though my second problem is that it does not seem as though the application is following my new routing rule for the only controller that uses this "session-less" handler:

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "FirstController", action = "Index", id = UrlParameter.Optional }
    );
        routes.Add("Sessionless", new Route("SessionlessController/SessionlessAction/{id}",
                new RouteValueDictionary(new { id= UrlParameter.Optional }),
                new SessionlessHandler()));

我的处理程序上有一个断点,尽管该应用程序似乎没有转到处理程序:

I have a breakpoint on my handler, though the app does not seem to go to the handler:

public class SessionlessHandler: IRouteHandler {
    public IHttpHandler GetHttpHandler(RequestContext requestContext) {
        return null;
    }
}

我希望只有我的无会话"控制器将使用此自定义处理程序,而所有其他控件都使用默认设置.

I want it that only my "session-less" controller will use this custom handler and all other controls use default.

推荐答案

您需要颠倒路线的顺序,以便最通用的路线在通用 Default 之前执行 路线.

You need to reverse the order of your routes so the most specific route executes before your general Default route.

routes.Add("Sessionless", new Route("SessionlessController/SessionlessAction/{id}",
        new RouteValueDictionary(new { id= UrlParameter.Optional }),
        new SessionlessHandler()));

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "FirstController", action = "Index", id = UrlParameter.Optional }
);

有关完整说明,请参见为什么先在asp.net mvc中将特殊路由映射到普通路由之前.

See Why map special routes first before common routes in asp.net mvc for a complete explanation.

这篇关于无会话的HttpHandler并仅路由到一个控制器.NET的自定义处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆