我是ASP.NET MVC的新手.我之前使用过PHP,很容易创建会话并根据当前会话变量选择用户记录.
我在互联网上到处寻找一个简单的分步教程,可以向我展示如何在我的C#ASP.NET MVC 4应用程序中创建和使用会话.我想创建一个包含用户变量的会话,我可以从我的控制器中的任何位置访问它,并且能够在我的LINQ查询中使用变量.
-提前致谢!
我发现这个线程描述了一个非常有趣的OnSessionStart事件,但是我无法将它"挂钩"(无论它意味着什么)到我的global.asax.cs.在网上搜索时我也没有成功.那么有人可以向我解释一下,如果事件确实存在于asp.net mvc中,我在哪里得到它(继承或从何处获取)以及我在哪里放置它?
我想在asp.net mvc应用程序中的不同步骤中构造一个对象,每一步都是一个不同的页面.您在快速Web.Forms应用程序中存储在Session中的那种东西.
阅读它,Session在我看来并不像asp.net MVC的那样.但是我无法真正想到这种情况的其他替代方案,因为TempData和ViewData似乎也不合适,所以也许我错了.
当然,我可以将4个步骤放在一个页面中并显示/隐藏,但这不是我对问题的看法.我想听听你对MVC中Session的看法,如果它对于这种多步骤问题是一个很好的方法,或者你倾向于以其他方式做到这一点.
这非常类似于ASP.NET MVC中的Session变量问题,除了我不是在寻找如何访问Session,但是如果它是解决这个问题的最好方法,或者在Asp中有更好的东西.净MVC.
提前致谢
我在MVC中阅读了许多关于会话范围数据的帖子,但我仍然不清楚在解决方案中包含自定义会话包装器的正确位置.
我想从IPrincipal获取当前用户的用户名,加载有关该用户的其他信息并将其存储在Session中.然后我想从Controller和View访问该用户数据.
以下方法似乎都不符合我的要求.
选项1:直接访问Session集合
每个人似乎都认为这是一个坏主意,但老实说,这似乎是最简单的事情.但是,它不会使用户可用于视图.
public class ControllerBase : Controller {
public ControllerBase() : this(new UserRepository()) {}
public ControllerBase(IUserRepository userRepository) {
_userRepository = userRepository;
}
protected IUserRepository _userRepository = null;
protected const string _userSessionKey = "ControllerBase_UserSessionKey";
protected User {
get {
var user = HttpContext.Current.Session[_userSessionKey] as User;
if (user == null) {
var principal = this.HttpContext.User;
if (principal != null) {
user = _userRepository.LoadByName(principal.Identity.Name);
HttpContext.Current.Session[_userSessionKey] = user;
}
}
return user;
}
}
}
Run Code Online (Sandbox Code Playgroud)
选项2:将会话注入到类构造函数 论坛帖子中 …
问题陈述:
我正在尝试为MVC控制器中的会话对象分配一个值,因为对象引用没有设置为对象的实例,所以它给出异常.
我有两个控制器
当我在主控制器中为会话分配值时它工作正常.但是如果我在辅助控制器的Test()方法中分配相同的值,则它给出错误.
我在这里做错了什么???
主控制器:
public class MainController: Controller
{
SecondaryController secCont=new SecondaryController();
public ActionResult Index(LoginModel loginModel)
{
if (ModelState.IsValid)
{
Session["LogID"] = 10;
//This is working fine.
//Instead of this i want call secCont.Test(); method where value for session assigned, it is giving error.
}
return View(loginModel);
}
}
Run Code Online (Sandbox Code Playgroud)
二级控制器:
public class SecondaryController: Controller
{
public void Test()
{
Session["LogID"] = 10;
// Giving Error as **Object reference not …Run Code Online (Sandbox Code Playgroud) 我从ASP.NET MVC中的Session变量的答案中读到了一条评论.它建议使用HttpSessionStateWrapper而HttpSessionStateBase不是直接使用会话如下Session["MyValue"]:
如果正在使用ASP MVC,那么最好不要使用来自HttpContext.Current.Session的实际Session对象,而是使用System.Web.Abstractions.dll中的新HttpSessionStateWrapper和HttpSessionStateBase,然后使用Factory或DI来获取Session.
有人可以提供一个MVC示例,使用这两个上述类来初始化,检索和设置会话变量吗?
哪个是使用它的最佳方式.
HomeController : BaseController
{
var value1 = GetDataFromSession("key1")
SetDataInSession("key2",(object)"key2Value");
Or
var value2 = SessionWrapper.GetFromSession("key3");
GetFromSession.SetDataInSession("key4",(object)"key4Value");
}
Run Code Online (Sandbox Code Playgroud)
public class BaseController : Controller
{
public T GetDataFromSession<T>(string key)
{
return (T) HttpContext.Session[key];
}
public void SetDataInSession(string key, object value)
{
HttpContext.Session[key] = value;
}
}
Run Code Online (Sandbox Code Playgroud)
public class BaseController : Controller
{
public ISessionWrapper SessionWrapper { get; set; }
public BaseController()
{
SessionWrapper = new HttpContextSessionWrapper();
}
}
public interface ISessionWrapper
{
T GetFromSession<T>(string key);
void SetInSession(string key, …Run Code Online (Sandbox Code Playgroud) 假设我想存储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中处理会话的一些信息?
我在asp.net MVC5中创建了一个Web应用程序.在我的应用程序中,我有一些产品.在每个产品上,记录的用户都可以按下类似按钮.如果已记录的用户已按下它,我该如何禁用like按钮.我希望其他用户能够按下该按钮,但不是已经按下按钮的用户.
我不想在我的数据库中保存所有喜欢该产品的用户,因为我的数据库中有很多值.
任何人都可以给我一个建议我应该怎么做?至少我可以在会话中保存一些东西.
谢谢!