开发者

Webmatrix, Razor, and Sessions

I'm getting some funky behavior with session variables with Razor .cshtml pages. Essentially on some pages I check to see if a Session variable is set, and if not I set it. I keep getting a null object reference.

I belie开发者_如何转开发ve this is because ultimately Razor is designed for MVC, and I did a little reading about the MVC life cycle and apparently the session object is injected a a different point in the lifecyde then say asp.net webforms. So When I try to use it on my pages it isn't available yet.

So my question would be for simple sites based with Razor (not MVC yet, but moving that way) what would be an effective way to check if a session variable is set (like say "access level") that is checked fairly often. That would be part one of the question.

Part two of the question would be if this is bad practice, what then should I be doing to replace this approach?

Thank you for your input.


Razor view pages in ASP.NET MVC 3 execute in an almost identical manner to ASP.NET Web Pages with Razor syntax. And for the record, Razor was first developed for Web Pages and not for MVC - we later reworked it to make sure that it was a "first class citizen" in both scenarios.

As such, Session state should work just fine no matter where you use it - from within a Web Pages file, an MVC controller, or even an MVC view (though most would argue that using Session state from within an MVC view is a bad practice).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜