ASP.NET Web API 会话什么的? [英] ASP.NET Web API session or something?

查看:22
本文介绍了ASP.NET Web API 会话什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在会话(或 ASP.NET Web API 中的任何内容)中存储一些信息,我需要在每个 API 请求中检索这些信息.我们将有一个 api IIS 网站,并且将通过主机头添加多个网站绑定.当任何请求进入时,例如 api.xyz.com,主机头将被检查并将该网站信息存储在会话中,这些信息将在调用数据库时在每个后续 api 请求中使用.

I need to store some information in session(or in whatever in ASP.NET Web API) that I need to retrieve in every API request. We will have one api IIS web site and multiple web site binding will be added through host header. When any request comes in for example, api.xyz.com, host header will be checked and store that website information in session that will be used in each subsequent api request when making a call to database.

我知道 ASP.NET Web API 不支持会话.有没有其他方法来处理这种情况?我在哪里可以存储可以在每个后续请求中检索的信息?

I know there is no support for session in ASP.NET Web API. Is there any other way to handle this kind of situation? Where can I store information that can be retrieving in each subsequent request?

谢谢.

推荐答案

好吧,REST 的设计是无状态的.通过添加会话(或任何其他类型的东西),您将使其成为有状态的,并破坏了拥有 RESTful API 的任何目的.

Well, REST by design is stateless. By adding session (or anything else of that kind) you are making it stateful and defeating any purpose of having a RESTful API.

RESTful 服务的整体理念是每个资源都可以使用用于超媒体链接的通用语法进行唯一寻址,并且每个 HTTP 请求都应该携带足够的信息供其接收者处理它与 HTTP 的无状态性质完全一致".

The whole idea of RESTful service is that every resource is uniquely addressable using a universal syntax for use in hypermedia links and each HTTP request should carry enough information by itself for its recipient to process it to be in complete harmony with the stateless nature of HTTP".

因此,无论您在此处尝试使用 Web API 做什么,如果您希望拥有 RESTful API,很可能应该重新构建架构.

So whatever you are trying to do with Web API here, should most likely be re-architectured if you wish to have a RESTful API.

话虽如此,如果您仍然愿意沿着这条路走下去,有一种将会话添加到 Web API 的黑客方法,它已由 Imran 发布在这里 http://forums.asp.net/t/1780385.aspx/1

With that said, if you are still willing to go down that route, there is a hacky way of adding session to Web API, and it's been posted by Imran here http://forums.asp.net/t/1780385.aspx/1

代码(虽然我真的不推荐):

Code (though I wouldn't really recommend that):

public class MyHttpControllerHandler
  : HttpControllerHandler, IRequiresSessionState
{
    public MyHttpControllerHandler(RouteData routeData): base(routeData)
    { }
}

public class MyHttpControllerRouteHandler : HttpControllerRouteHandler
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MyHttpControllerHandler(requestContext.RouteData);
    }
}

public class ValuesController : ApiController
{
   public string GET(string input)
   {
       var session = HttpContext.Current.Session;
       if (session != null)
       {
           if (session["Time"] == null)
           {
               session["Time"] = DateTime.Now;
           }
           return "Session Time: " + session["Time"] + input;
       }
       return "Session is not availabe" + input;
    }
}

然后将 HttpControllerHandler 添加到您的 API 路由中:

and then add the HttpControllerHandler to your API route:

route.RouteHandler = new MyHttpControllerRouteHandler();

这篇关于ASP.NET Web API 会话什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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