ASP MVC,构建解决方案时会话丢失了吗? [英] Asp MVC, is session lost upon building solution?

查看:46
本文介绍了ASP MVC,构建解决方案时会话丢失了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在ASP.NET MVC中编写一个应用程序.基本上,我有一些页面需要用户验证.用户登录后,我将用户行保留在会话中.因此,在我的控制器中,无需进行额外查询即可访问user.ID.

I 'm writting an application in ASP.NET MVC. Basically I have some pages which require user authentication. Upon user logon, I keep the user row in a session. So in my controller I can access to the user.ID without making extra queries.

当项目处于调试模式时,我只能在视图中进行更改.不在控制器中.

When project is in debug mode I can only change things in the views. Not in the controller.

如果不进行调试,则可以构建解决方案,并在不运行项目(使用F5)的情况下查看所做的更改.但是,它会丢失我拥有的所有会话变量.

If I 'm not debugging, I can build the solution, and see the changes I made without running the project (with F5). BUT, it looses all session variables I have.

因此,基本上,对于控制器中的每一个细微变化,我都必须注销,登录才能看到我的更改.

So basically for every no-matter how small change in the controller, I have to logoff, logon to see my changes.

那些正常的行为吗?

推荐答案

就像Dan所说的,这是正常现象.使它更容易(并且稍微更健壮)是稍微更改代码.当然,这是假设您在会话中存储的不仅是用户ID,因为在验证用户身份时,您可以通过 Controller.User.Identity.Name 访问用户ID.因此,您可以在会话对象中执行其他数据的查找,如果它不返回null,请使用它.如果确实返回null,则根据用户ID再次查找附加信息,并将其再次存储在会话中.这是我用于从Active Directory存储信息的方法,并且效果很好.

Like Dan stated, this is normal behavior. To make it easier (and slightly more robust) is to change your code slightly. This is of course assuming that you are storing more than just the User ID in session since you can access the User ID via Controller.User.Identity.Name when they are authenticated. So you perform the lookup of the additional data in the session object and if it doesn't return null then use it. If it does return null, then look up the additional information again based on the User ID and store it in the session again. This is the approach I take for storing information from Active Directory and it works great.

这篇关于ASP MVC,构建解决方案时会话丢失了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆