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

查看:96
本文介绍了的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的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".

所以,无论你想在这里与网页API做的,应该最有可能被重新architectured如果你想有一个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的方式哈克,而且它已经张贴在这里伊姆兰的 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(虽然我不会真的建议):

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天全站免登陆