我需要在每个API请求中检索我需要检索的会话(或ASP.NET Web API中的任何内容)中存储一些信息.我们将有一个api IIS网站,并将通过主机头添加多个网站绑定.当任何请求进入时,例如api.xyz.com,将检查主机头并将该网站信息存储在会话中,该会话将在调用数据库时在每个后续api请求中使用.
我知道ASP.NET Web API中没有对会话的支持.有没有其他方法来处理这种情况?我在哪里可以存储可以在每个后续请求中检索的信息?
谢谢.
如果没有路由,HttpContext.Current.Session那么我知道它StateServer正在运行.当我路由我的请求时,HttpContext.Current.Session是null在路由页面中.我在IIS 7.0上使用.NET 3.5 sp1,没有MVC预览.似乎AcquireRequestState在使用路由时从不触发,因此会话变量未实例化/填充.
当我尝试访问Session变量时,我收到此错误:
base {System.Runtime.InteropServices.ExternalException} = {"Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>.
在调试时,我也得到了HttpContext.Current.Session在该上下文中无法访问的错误.
-
我web.config看起来像这样:
<configuration>
...
<system.web>
<pages enableSessionState="true">
<controls>
...
</controls>
</pages>
...
</system.web>
<sessionState cookieless="AutoDetect" mode="StateServer" timeout="22" /> …Run Code Online (Sandbox Code Playgroud) 我目前在ASP .NET MVC 5项目中有两个区域.一个叫做支持者,一个叫做查特.在这两个区域中的每一个区域中都有一个ApiController名称CommunicationController,由于ApiController路由的工作性质,这会产生问题.
如果我只在一个区域中ApiController命名CommunicationController了一个,它的路由将不包括URL中的区域,并且URL将是这样的:
http://example.com/api/communication/someAction
但在上面的URL中,该区域在哪里?
由于我的两个控制器命名相同,它们现在都有路由问题.
我尝试按照这里的说明操作:http://blogs.infosupport.com/asp-net-mvc-4-rc-getting-webapi-and-areas-to-play-nicely/
它们似乎适用于ASP .NET MVC 4 RC,由于我使用的是MVC 5,因此它不再具有相关性,这可能就是它无法正常工作的原因.
但是,从该博文中回顾一下,这是我的路由文件.
App_Start\RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Default", "{controller}/{action}",
new {action = "Index", controller = "Home"},
new[] { "Website.Controllers" }
);
}
}
Run Code Online (Sandbox Code Playgroud)
App_Start\WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
//the two lines below …Run Code Online (Sandbox Code Playgroud)