如何实现特定的控制器会知道WEB API? [英] How do I implement session aware WEB API on specific controllers?

查看:222
本文介绍了如何实现特定的控制器会知道WEB API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造一些网络API 2控制器,被需要是会话感知。我已经previous加入做到了这一点。

I am creating some WEB API 2 controllers that are needing to be session aware. I have previous done this by adding

/// <summary>
/// Application_s the post authorize request.
/// </summary>
protected void Application_PostAuthorizeRequest()
{
     HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}

然而,我们在对已高度优化网站其它业务关键部分的溶液的API控制器和正在返回的周围500ms的响应,如果这被接通它始终上升到2秒。这些控制器不需要会话感知。

However we have API controllers in the solution for other business critical parts of the site that have been highly optimized and are returning responses of around 500ms and if this is turned on it consistently goes up to 2 seconds. These controllers do not need session awareness.

我们只需要给出会话访问某些控制器,我读过这篇文章的 HTTP://www.$c$cproject.com/Tips/513522/Providing-session-state-in-ASP-NET-WebAPI 并在想,如果有可能添加与会话感知不同的路线,但没有 RouteHandler 映射路由时属性。

We only need certain controllers given session access, I've read this article http://www.codeproject.com/Tips/513522/Providing-session-state-in-ASP-NET-WebAPI and was thinking if it's possible to add a different route with session awareness but there is no RouteHandler property when mapping the routes.

有没有人有什么想法?

推荐答案

我想出了哪些工作的解决方案。我已经添加路由注册在第二路径,如

I came up with a solution which worked. I have added a second route during the routes registration such as

        config.Routes.MapHttpRoute(
            name: "DefaultSessionApi",
            routeTemplate: "sessionapi/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional });

然后在我的global.asax.cs有

Then in the global.asax.cs I have

    /// <summary>
    /// Application_s the post authorize request.
    /// </summary>
    protected void Application_PostAuthorizeRequest()
    {
        if (HttpContext.Current.Request.FilePath.StartsWith("/sessionapi"))
        {
            HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
        }
    }

这允许任何控制器与会话感知或未经客户因此解决了我的问题的请求。

This allows any controller to be request with session awareness or without from the client therefore solving my issue.

我的解决办法是配置常数等,但这种高于code为例子。

My solution is a little neater with config constants and such but this above code is the example.

这篇关于如何实现特定的控制器会知道WEB API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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