A demo for use redis as Session for ASP.NET web application
- Require "Microsoft.AspNet.Mvc" by (
Install-Package Microsoft.AspNet.Mvc) - Require "Microsoft.AspNet.WebApi" by (
Install-Package Microsoft.AspNet.WebApi) - Require "ServiceStack.Redis" by (
Install-Package ServiceStack.Redis)
- Add "RedisSession4Net.Core.dll" reference to your ASP.NET web project.
- In Web.Config add RedisConfig section as below:
<configSections>
<section name="RedisConfig" type="RedisSession4Net.Core.Cache.RedisConfig" />
</configSections>- Before the end or 'configuration' section in Web.config, add your redis service info as below:
<RedisConfig
ReadWriteServerStr="127.0.0.1:6379"
ReadOnlyServerStr="127.0.0.1:6379"
LocalCacheTime="1800" />- Note: if your redis service require password authentication, just set
ReadWriteServerStr\ReadOnlyServerStraspassword@host:6379.
- Add a
SessionModelclass as demo. - Make your WebApi Controller inherit
RedisSession4Net.Core.Components.BaseApiController, and useSessionModel(in Step:#4) as generic base type. - Now, you can use redis as session for WebApi as below:
this.Session.SampleString += id.ToString() + ";";This project is licensed under the MIT License - see the LICENSE.md file for details