如何在asp.net mvc中仅更改一条路由的会话?

发布于 2024-09-02 08:35:45 字数 890 浏览 5 评论 0原文

如何在asp.net mvc中使用自定义过滤器处理Application_BeginRequest?

我只想恢复一条路由(~/my-url)的会话。

如果我可以创建一个自定义过滤器并处理它,那就太酷了。

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var context = HttpContext.Current;

        if (string.Equals("~/my-url",
            context.Request.AppRelativeCurrentExecutionFilePath, 
            StringComparison.OrdinalIgnoreCase))
        {
            string sessionId = context.Request.Form["sessionId"];

            if (sessionId != null)
            {
                HttpCookie cookie = context.Request.Cookies.Get("ASP.NET_SessionId");
                if (cookie == null)
                {
                    cookie = new HttpCookie("ASP.NET_SessionId");
                }
                cookie.Value = sessionId;
                context.Request.Cookies.Set(cookie);
            }
        }

How to handle Application_BeginRequest using a custom filter in asp.net mvc?

I want to restore session only for one route (~/my-url).

It would be cool, if I could create a custom filter and handle that.

protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var context = HttpContext.Current;

        if (string.Equals("~/my-url",
            context.Request.AppRelativeCurrentExecutionFilePath, 
            StringComparison.OrdinalIgnoreCase))
        {
            string sessionId = context.Request.Form["sessionId"];

            if (sessionId != null)
            {
                HttpCookie cookie = context.Request.Cookies.Get("ASP.NET_SessionId");
                if (cookie == null)
                {
                    cookie = new HttpCookie("ASP.NET_SessionId");
                }
                cookie.Value = sessionId;
                context.Request.Cookies.Set(cookie);
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2024-09-09 08:35:45

我使用 IRouteHandler 做到了。

public class SessionHandler : IRouteHandler
{
    public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        string sessionId = requestContext.HttpContext.Request.Form["sessionId"];

        if (sessionId != null)
        {
            HttpCookie cookie = requestContext.HttpContext.Request.Cookies.Get("ASP.NET_SessionId");
            if (cookie == null)
            {
                cookie = new HttpCookie("ASP.NET_SessionId");
            }
            cookie.Value = sessionId;
            requestContext.HttpContext.Request.Cookies.Set(cookie);
        }


        return new MvcHandler(requestContext);
    }
}

这是在 global.asax 中(abc/qwr 是路线):

RouteTable.Routes.Add(new Route(
                "abc/qwr",
                new RouteValueDictionary(new {controller = "MyController", action = "MyAction"}),
                new RouteValueDictionary(),
                new RouteValueDictionary(new { Namespaces = new[] { typeof(MyControllerController).Namespace } }),
                new SessionHandler()
        ));

有什么意见吗?

I did it using IRouteHandler.

public class SessionHandler : IRouteHandler
{
    public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        string sessionId = requestContext.HttpContext.Request.Form["sessionId"];

        if (sessionId != null)
        {
            HttpCookie cookie = requestContext.HttpContext.Request.Cookies.Get("ASP.NET_SessionId");
            if (cookie == null)
            {
                cookie = new HttpCookie("ASP.NET_SessionId");
            }
            cookie.Value = sessionId;
            requestContext.HttpContext.Request.Cookies.Set(cookie);
        }


        return new MvcHandler(requestContext);
    }
}

This is in global.asax (abc/qwr is the route):

RouteTable.Routes.Add(new Route(
                "abc/qwr",
                new RouteValueDictionary(new {controller = "MyController", action = "MyAction"}),
                new RouteValueDictionary(),
                new RouteValueDictionary(new { Namespaces = new[] { typeof(MyControllerController).Namespace } }),
                new SessionHandler()
        ));

Any comments?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文