Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 Azure MVC4(测试版)WebApi Global.asax会话\u未调用启动?WebApi会话状态访问?_Asp.net Mvc 4_Session State_Asp.net Web Api - Fatal编程技术网

Asp.net mvc 4 Azure MVC4(测试版)WebApi Global.asax会话\u未调用启动?WebApi会话状态访问?

Asp.net mvc 4 Azure MVC4(测试版)WebApi Global.asax会话\u未调用启动?WebApi会话状态访问?,asp.net-mvc-4,session-state,asp.net-web-api,Asp.net Mvc 4,Session State,Asp.net Web Api,我的MVC4(beta版)Azure WebRole具有用于WebApi访问的控制器。这很好,除了一个小问题:似乎只有在我访问MVC页面(例如HomeController)时才会调用Global.asax.cs Session_Start 场景:我的WebApi控制器需要访问(进程中)会话状态存储。我在Start_会话中访问会话状态对象,并缓存它以供WebApi控制器使用。只要我先访问单个MVC网页,这就可以正常工作,但如果我先访问WebApi,我的会话状态缓存不会初始化,但显然路由表是初始化的

我的MVC4(beta版)Azure WebRole具有用于WebApi访问的控制器。这很好,除了一个小问题:似乎只有在我访问MVC页面(例如HomeController)时才会调用Global.asax.cs Session_Start

场景:我的WebApi控制器需要访问(进程中)会话状态存储。我在Start_会话中访问会话状态对象,并缓存它以供WebApi控制器使用。只要我先访问单个MVC网页,这就可以正常工作,但如果我先访问WebApi,我的会话状态缓存不会初始化,但显然路由表是初始化的,因此正在访问Global.asax

在这种情况下,有关于初始化或会话状态访问的提示吗?我需要在不首先访问网页的情况下访问WebApi控制器

谢谢!
R

这里是ASP.NET上的论坛帖子,展示了如何使用PostAuthorizerRequest方法为WebApi调用启用会话状态。我已将下面的代码片段包含在对Global.asax文件的修改中,供您参考

private const string _WebApiPrefix = "api";
private static string _WebApiExecutionPath = String.Format("~/{0}", _WebApiPrefix);

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: String.Format("{0}/{{controller}}/{{id}}", _WebApiPrefix),
      defaults: new { id = RouteParameter.Optional }
  );

}

protected void Application_PostAuthorizeRequest()
{
  if (IsWebApiRequest())
  {
    HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
  }
}

private static bool IsWebApiRequest()
{
  return HttpContext.Current.Request
       .AppRelativeCurrentExecutionFilePath.StartsWith(_WebApiExecutionPath);
}

看起来这应该可以解决您的问题。

这里是ASP.NET上的论坛帖子-展示了如何使用PostAuthorizerRequest方法为WebApi调用启用会话状态。我已将下面的代码片段包含在对Global.asax文件的修改中,供您参考

private const string _WebApiPrefix = "api";
private static string _WebApiExecutionPath = String.Format("~/{0}", _WebApiPrefix);

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: String.Format("{0}/{{controller}}/{{id}}", _WebApiPrefix),
      defaults: new { id = RouteParameter.Optional }
  );

}

protected void Application_PostAuthorizeRequest()
{
  if (IsWebApiRequest())
  {
    HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
  }
}

private static bool IsWebApiRequest()
{
  return HttpContext.Current.Request
       .AppRelativeCurrentExecutionFilePath.StartsWith(_WebApiExecutionPath);
}
看起来这应该可以解决您的问题。

在Global.asax中

public override void Init()
{
    this.PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
    base.Init();
}

void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
    System.Web.HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
在Global.asax中

public override void Init()
{
    this.PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
    base.Init();
}

void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
    System.Web.HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}

当然有。谢谢你的提示!当然有。谢谢你的提示!