Tom*_*ghe 2 c# asp.net-mvc session
假设我想存储language_id会话中调用的变量.我想我可能会做以下事情:
public class CountryController : Controller
{
[WebMethod(EnableSession = true)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResultChangelangue(FormCollection form)
{
Session["current_language"] = form["languageid"];
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我检查会话时,它总是为空.怎么会?我在哪里可以找到有关ASP.NET MVC中处理会话的一些信息?
Dan*_*son 12
与问题本身并不严格相关,但更多的是作为一种保持控制器(合理地)强类型和清洁的方式,我还建议使用类似于会话的会话外观,其中包含任何会话信息,以便您在其中读取和写入好方法.
例:
public static class SessionFacade
{
public static string CurrentLanguage
{
get
{
//Simply returns, but you could check for a null
//and initialise it with a default value accordingly...
return HttpContext.Current.Session["current_language"].ToString();
}
set
{
HttpContext.Current.Session["current_language"] = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
public ActionResultChangelangue(FormCollection form)
{
SessionFacade.CurrentLanguage = form["languageid"];
return View();
}
Run Code Online (Sandbox Code Playgroud)