自从使用Asp.net Core2.0 以来,不停摸索,查阅资料,这方面的资料是真的少,因此,在前人的基础上,摸索出了Asp.net Core2.0 缓存 MemoryCache 和 Redis的用法,并实现了简单的封装

那么,先给出几个参考资料吧

关于两种缓存:https://www.cnblogs.com/yuangang/p/5800113.html

关于redis持久化:https://blog.csdn.net/u010785685/article/details/52366977

两个nuget包,不用多说

接下来,贴出代码

首先在startup,我读取了appstting.json的数据,作为redis的配置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Common;
using Application.Common.CommonObject;
using Application.CoreWork;
using Application.DAL;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; namespace Application.web
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
Connection.MySqlConnection = Configuration.GetConnectionString("MySqlConnection");
Connection.SqlConnection = Configuration.GetConnectionString("SqlConnection");
RedisConfig.Connection = Configuration.GetSection("RedisConfig")["Connection"];
RedisConfig.DefaultDatabase =Convert.ToInt32( Configuration.GetSection("RedisConfig")["DefaultDatabase"]);
RedisConfig.InstanceName = Configuration.GetSection("RedisConfig")["InstanceName"];
CommonManager.CacheObj.GetMessage<RedisCacheHelper>();
services.AddMvc();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
} app.UseStaticFiles(); app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}

再贴上配置

"RedisConfig": {
"Connection": "127.0.0.1:6379",
"DefaultDatabase": 0,
"InstanceName": "Redis1"
},

  

用来放配置信息的静态类

using System;
using System.Collections.Generic;
using System.Text; namespace Application.Common
{
public static class RedisConfig
{
public static string configname { get; set; }
public static string Connection { get; set; }
public static int DefaultDatabase { get; set; }
public static string InstanceName { get; set; }
}
}

然后ICacheHelper.cs,这个类是两种缓存通用的接口,让使用更方便

using System;
using System.Collections.Generic;
using System.Net;
using System.Text; namespace Application.Common.CommonObject
{
public interface ICacheHelper
{
bool Exists(string key); T GetCache<T>(string key) where T : class; void SetCache(string key, object value); void SetCache(string key, object value, DateTimeOffset expiressAbsoulte);//设置绝对时间过期 void SetSlidingCache(string key, object value, TimeSpan t); //设置滑动过期, 因redis暂未找到自带的滑动过期类的API,暂无需实现该接口 void RemoveCache(string key); void KeyMigrate(string key, EndPoint endPoint,int database,int timeountseconds); void Dispose(); void GetMssages(); void Publish(string msg);
}
}

然后,实现接口,首先是MemoryCacheHelper

using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text; namespace Application.Common.CommonObject
{
public class MemoryCacheHelper : ICacheHelper
{
//public MemoryCacheHelper(/*MemoryCacheOptions options*/)//这里可以做成依赖注入,但没打算做成通用类库,所以直接把选项直接封在帮助类里边
//{
// //this._cache = new MemoryCache(options);
// //this._cache = new MemoryCache(new MemoryCacheOptions());
//}
//public MemoryCacheHelper(MemoryCacheOptions options)//这里可以做成依赖注入,但没打算做成通用类库,所以直接把选项直接封在帮助类里边
//{
// this._cache = new MemoryCache(options);
//} public static IMemoryCache _cache=new MemoryCache(new MemoryCacheOptions()); /// <summary>
/// 是否存在此缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Exists(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
object v = null;
return _cache.TryGetValue<object>(key, out v);
}
/// <summary>
/// 取得缓存数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T GetCache<T>(string key) where T : class
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key)); T v = null;
_cache.TryGetValue<T>(key, out v); return v;
}
/// <summary>
/// 设置缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void SetCache(string key, object value)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
if (value == null)
throw new ArgumentNullException(nameof(value)); object v = null;
if (_cache.TryGetValue(key, out v))
_cache.Remove(key);
_cache.Set<object>(key, value);
}
/// <summary>
/// 设置缓存,绝对过期
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expirationMinute">间隔分钟</param>
/// CommonManager.CacheObj.Save<RedisCacheHelper>("test", "RedisCache works!", 30);
public void SetCache(string key, object value, double expirationMinute)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
if (value == null)
throw new ArgumentNullException(nameof(value)); object v = null;
if (_cache.TryGetValue(key, out v))
_cache.Remove(key);
DateTime now = DateTime.Now;
TimeSpan ts = now.AddMinutes(expirationMinute) - DateTime.Now;
_cache.Set<object>(key, value, ts);
}
/// <summary>
/// 设置缓存,绝对过期
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expirationTime">DateTimeOffset 结束时间</param>
/// CommonManager.CacheObj.Save<RedisCacheHelper>("test", "RedisCache works!", DateTimeOffset.Now.AddSeconds(30));
public void SetCache(string key, object value, DateTimeOffset expirationTime)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
if (value == null)
throw new ArgumentNullException(nameof(value)); object v = null;
if (_cache.TryGetValue(key, out v))
_cache.Remove(key); _cache.Set<object>(key, value, expirationTime);
}
/// <summary>
/// 设置缓存,相对过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="t"></param>
/// CommonManager.CacheObj.SaveSlidingCache<MemoryCacheHelper>("test", "MemoryCache works!",TimeSpan.FromSeconds(30));
public void SetSlidingCache(string key,object value,TimeSpan t)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
if (value == null)
throw new ArgumentNullException(nameof(value)); object v = null;
if (_cache.TryGetValue(key, out v))
_cache.Remove(key); _cache.Set(key, value, new MemoryCacheEntryOptions()
{
SlidingExpiration=t
});
}
/// <summary>
/// 移除缓存
/// </summary>
/// <param name="key"></param>
public void RemoveCache(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key)); _cache.Remove(key);
} /// <summary>
/// 释放
/// </summary>
public void Dispose()
{
if (_cache != null)
_cache.Dispose();
GC.SuppressFinalize(this);
} public void KeyMigrate(string key, EndPoint endPoint, int database, int timeountseconds)
{
throw new NotImplementedException();
} public void GetMssages()
{
throw new NotImplementedException();
} public void Publish(string msg)
{
throw new NotImplementedException();
}
}
}

然后是RedisCacheHelper

using Microsoft.Extensions.Caching.Redis;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text; namespace Application.Common.CommonObject
{
public class RedisCacheHelper : ICacheHelper
{
public RedisCacheHelper(/*RedisCacheOptions options, int database = 0*/)//这里可以做成依赖注入,但没打算做成通用类库,所以直接把连接信息直接写在帮助类里
{
options = new RedisCacheOptions();
options.Configuration = "127.0.0.1:6379";//RedisConfig.Connection;
options.InstanceName = RedisConfig.InstanceName;
int database = RedisConfig.DefaultDatabase;
_connection = ConnectionMultiplexer.Connect(options.Configuration);
_cache = _connection.GetDatabase(database);
_instanceName = options.InstanceName;
_sub = _connection.GetSubscriber();
} public static RedisCacheOptions options;
public static IDatabase _cache; public static ConnectionMultiplexer _connection; public static string _instanceName; public static ISubscriber _sub; /// <summary>
/// 取得redis的Key名称
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private string GetKeyForRedis(string key)
{
return _instanceName + key;
}
/// <summary>
/// 判断当前Key是否存在数据
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Exists(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
return _cache.KeyExists(GetKeyForRedis(key));
} /// <summary>
/// 取得缓存数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T GetCache<T>(string key) where T : class
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
var value = _cache.StringGet(GetKeyForRedis(key));
if (!value.HasValue)
return default(T);
return JsonConvert.DeserializeObject<T>(value);
}
/// <summary>
/// 设置缓存数据
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void SetCache(string key, object value)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
if (value == null)
throw new ArgumentNullException(nameof(value)); if (Exists(GetKeyForRedis(key)))
RemoveCache(GetKeyForRedis(key)); _cache.StringSet(GetKeyForRedis(key), JsonConvert.SerializeObject(value));
}
/// <summary>
/// 设置绝对过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiressAbsoulte"></param>
public void SetCache(string key, object value, DateTimeOffset expiressAbsoulte)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
if (value == null)
throw new ArgumentNullException(nameof(value)); if (Exists(GetKeyForRedis(key)))
RemoveCache(GetKeyForRedis(key));
TimeSpan t = expiressAbsoulte - DateTimeOffset.Now;
_cache.StringSet(GetKeyForRedis(key), JsonConvert.SerializeObject(value), t);
}
/// <summary>
/// 设置相对过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expirationMinute"></param>
public void SetCache(string key, object value, double expirationMinute)
{
if (Exists(GetKeyForRedis(key)))
RemoveCache(GetKeyForRedis(key)); DateTime now = DateTime.Now;
TimeSpan ts = now.AddMinutes(expirationMinute) - now;
_cache.StringSet(GetKeyForRedis(key), JsonConvert.SerializeObject(value), ts);
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="endPoint"></param>
/// <param name="database"></param>
/// <param name="timeountseconds"></param>
public void KeyMigrate(string key, EndPoint endPoint, int database, int timeountseconds) {
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));
_cache.KeyMigrate(GetKeyForRedis(key), endPoint, database, timeountseconds);
}
/// <summary>
/// 移除redis
/// </summary>
/// <param name="key"></param>
public void RemoveCache(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key)); _cache.KeyDelete(GetKeyForRedis(key));
} /// <summary>
/// 销毁连接
/// </summary>
public void Dispose()
{
if (_connection != null)
_connection.Dispose();
GC.SuppressFinalize(this);
} public void SetSlidingCache(string key, object value, TimeSpan t)
{
throw new NotImplementedException();
} public void GetMssages()
{
using (_connection = ConnectionMultiplexer.Connect(options.Configuration))
_sub.Subscribe("msg", (channel, message) =>
{
string result = message;
});
} public void Publish(string msg)
{
using (_connection=ConnectionMultiplexer.Connect(options.Configuration))
_sub.Publish("msg", msg);
}
}
}

然后是Cache.cs,用来实例化,此处用单例模式,保证项目使用的是一个缓存实例,博主吃过亏,由于redis实例过多,构造函数运行太多次,导致client连接数过大,内存不够跑,速度卡慢

using System;
using System.Collections.Generic;
using System.Text; namespace Application.Common.CommonObject
{
public sealed class Cache
{
internal Cache() { }
public static ICacheHelper cache;
/// <summary>
/// 判断缓存是否存在
/// </summary>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">键名</param>
/// <returns></returns>
public bool Exists<CacheType>(string key) where CacheType : ICacheHelper, new()
{
if (cache!=null&& typeof(CacheType).Equals(cache.GetType()))
return cache.Exists(key);
else
{
cache = new CacheType();
return cache.Exists(key);
}
} /// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T">转换的类</typeparam>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">键名</param>
/// <returns>转换为T类型的值</returns>
public T GetCache<T,CacheType>(string key)
where T:class
where CacheType : ICacheHelper, new()
{
if (cache != null && typeof(CacheType).Equals(cache.GetType()))
return cache.GetCache<T>(key);
else
{
cache = new CacheType();
return cache.GetCache<T>(key);
}
} /// <summary>
/// 保存缓存
/// </summary>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">键名</param>
/// <param name="value">值</param>
public void Save<CacheType>(string key,object value) where CacheType : ICacheHelper, new()
{
if (cache != null && typeof(CacheType).Equals(cache.GetType()))
cache.SetCache(key, value);
else
{
cache = new CacheType();
cache.SetCache(key, value);
}
} /// <summary>
/// 保存缓存并设置绝对过期时间
/// </summary>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">键名</param>
/// <param name="value">值</param>
/// <param name="expiressAbsoulte">绝对过期时间</param>
public void Save<CacheType>(string key, object value, DateTimeOffset expiressAbsoulte) where CacheType : ICacheHelper, new()
{
if (cache != null && typeof(CacheType).Equals(cache.GetType()))
cache.SetCache(key, value, expiressAbsoulte);
else
{
cache = new CacheType();
cache.SetCache(key, value, expiressAbsoulte);
}
} /// <summary>
/// 保存滑动缓存
/// </summary>
/// <typeparam name="CacheType">只能用memorycache,redis暂不实现</typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="t">间隔时间</param>
public void SaveSlidingCache<CacheType>(string key, object value,TimeSpan t) where CacheType : MemoryCacheHelper, new()
{
if (cache != null && typeof(CacheType).Equals(cache.GetType()))
cache.SetSlidingCache(key, value, t);
else
{
cache = new CacheType();
cache.SetSlidingCache(key, value, t);
}
} /// <summary>
/// 删除一个缓存
/// </summary>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">要删除的key</param>
public void Delete<CacheType>(string key)where CacheType : ICacheHelper, new()
{
if (cache != null && typeof(CacheType).Equals(cache.GetType()))
cache.RemoveCache(key);
else
{
cache = new CacheType();
cache.RemoveCache(key);
}
} /// <summary>
/// 释放
/// </summary>
/// <typeparam name="CacheType"></typeparam>
public void Dispose<CacheType>() where CacheType : ICacheHelper, new()
{
if (cache != null && typeof(CacheType).Equals(cache.GetType()))
cache.Dispose();
else
{
cache = new CacheType();
cache.Dispose();
}
} public void GetMessage<CacheType>() where CacheType:RedisCacheHelper,new()
{
if (cache != null && typeof(CacheType).Equals(cache.GetType()))
cache.GetMssages();
else
{
cache = new CacheType();
cache.GetMssages();
}
} public void Publish<CacheType>(string msg)where CacheType : RedisCacheHelper, new()
{
if (cache != null && typeof(CacheType).Equals(cache.GetType()))
cache.Publish(msg);
else
{
cache = new CacheType();
cache.Publish(msg);
}
}
}
}

接下来,CommonManager.cs,该类主要是为了作为一个静态类调用缓存,由于Cache.cs并非静态类,方便调用

using Application.Common.CommonObject;
using System;
using System.Collections.Generic;
using System.Text; namespace Application.Common
{
public class CommonManager
{
private static readonly object lockobj = new object();
private static volatile Cache _cache = null;
/// <summary>
/// Cache
/// </summary>
public static Cache CacheObj
{
get
{
if (_cache == null)
{
lock (lockobj)
{
if (_cache == null)
_cache = new Cache();
}
}
return _cache;
}
}
}
}

最后呢就是使用啦随便建的一个控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Application.Common;
using Application.Common.CommonObject;
using Application.CoreWork;
using Microsoft.AspNetCore.Mvc; namespace Application.web.Controllers
{
public class DefaultController : BaseController
{
public IActionResult Index()
{
for (int i = ; i < ; i++)
{
CommonManager.CacheObj.Save<RedisCacheHelper>("key" + i, "key" + i + " works!");
}
return View();
} public IActionResult GetStrin(int index)
{
string res = "已经过期了";
if (CommonManager.CacheObj.Exists<RedisCacheHelper>("key" + index))
{
res = CommonManager.CacheObj.GetCache<String, RedisCacheHelper>("key" + index);
}
return Json(new ExtJson { success = true, code = , msg = "成功", jsonresult = res });
} public IActionResult Publish(string msg)
{
try
{
CommonManager.CacheObj.Publish<RedisCacheHelper>(msg);
return Json(new ExtJson { success = true, code = , msg = "成功", jsonresult = msg });
}
catch
{
return Json(new ExtJson
{
success = true,
code = ,
msg = "失败",
jsonresult = msg
});
}
}
}
}

那么,有的朋友在json处可能会报错,自己封装下,或者用原来的json吧

如果有不懂的地方,可以在评论中提问,有更好的改进方式,也请联系我,共同进步,感谢阅读

2019-5-23更新

在实际运行过程中,发现大量用户请求,并且两种缓存混合使用时,redis和memory的单例会多次创建,导致redis连接数达到上限,下面贴出修改后的cache.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text; namespace Application.Common.CommonObject
{
public sealed class Cache
{
internal Cache() { }
public static ICacheHelper cache; public static RedisCacheHelper redis;
public static MemoryCacheHelper memory; public ICacheHelper Getcachehelper<CacheType>()
{
if (typeof(CacheType).Equals(typeof(RedisCacheHelper)))
{
if (redis == null)
{
redis = new RedisCacheHelper();
Process pc = Process.GetCurrentProcess();
CommonManager.TxtObj.Log4netError("进程ID:"+pc.Id+";redis为null,创建一个新的实例",typeof(Cache),null);
}
return redis;
}
else if (typeof(CacheType).Equals(typeof(MemoryCacheHelper)))
{
if (memory == null)
memory = new MemoryCacheHelper();
return memory;
}
else
return null;
}
/// <summary>
/// 判断缓存是否存在
/// </summary>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">键名</param>
/// <returns></returns>
public bool Exists<CacheType>(string key) where CacheType : ICacheHelper, new()
{
cache = Getcachehelper<CacheType>();
return cache.Exists(key);
} /// <summary>
/// 获取缓存
/// </summary>
/// <typeparam name="T">转换的类</typeparam>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">键名</param>
/// <returns>转换为T类型的值</returns>
public T GetCache<T, CacheType>(string key)
where T : class
where CacheType : ICacheHelper, new()
{
cache = Getcachehelper<CacheType>();
return cache.GetCache<T>(key);
} /// <summary>
/// 保存缓存
/// </summary>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">键名</param>
/// <param name="value">值</param>
public void Save<CacheType>(string key, object value) where CacheType : ICacheHelper, new()
{
cache = Getcachehelper<CacheType>();
cache.SetCache(key, value);
} /// <summary>
/// 保存缓存并设置绝对过期时间
/// </summary>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">键名</param>
/// <param name="value">值</param>
/// <param name="expiressAbsoulte">绝对过期时间</param>
public void Save<CacheType>(string key, object value, DateTimeOffset expiressAbsoulte) where CacheType : ICacheHelper, new()
{
cache = Getcachehelper<CacheType>();
cache.SetCache(key, value, expiressAbsoulte);
} /// <summary>
/// 保存滑动缓存
/// </summary>
/// <typeparam name="CacheType">只能用memorycache,redis暂不实现</typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="t">间隔时间</param>
public void SaveSlidingCache<CacheType>(string key, object value, TimeSpan t) where CacheType : MemoryCacheHelper, new()
{
cache = Getcachehelper<CacheType>();
cache.SetSlidingCache(key, value, t);
} /// <summary>
/// 删除一个缓存
/// </summary>
/// <typeparam name="CacheType">缓存类型</typeparam>
/// <param name="key">要删除的key</param>
public void Delete<CacheType>(string key) where CacheType : ICacheHelper, new()
{
cache = Getcachehelper<CacheType>();
cache.RemoveCache(key);
} /// <summary>
/// 释放
/// </summary>
/// <typeparam name="CacheType"></typeparam>
public void Dispose<CacheType>() where CacheType : ICacheHelper, new()
{
cache = Getcachehelper<CacheType>();
cache.Dispose();
}
public List<string> GetCacheKeys<CacheType>() where CacheType : ICacheHelper, new()
{
cache = Getcachehelper<CacheType>();
return cache.GetCacheKeys();
}
public void GetMessage<CacheType>() where CacheType : RedisCacheHelper, new()
{
cache = Getcachehelper<CacheType>();
cache.GetMssages();
} public void Publish<CacheType>(string msg) where CacheType : RedisCacheHelper, new()
{
cache = Getcachehelper<CacheType>();
cache.Publish(msg);
}
}
}

  

Asp.net Core2.0 缓存 MemoryCache 和 Redis的更多相关文章

  1. Asp.net Core 缓存 MemoryCache 和 Redis

    Asp.net Core 缓存 MemoryCache 和 Redis 目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 经过 N 久反复的尝试,翻阅了网上无数的资料,GitH ...

  2. 一步一步带你做WebApi迁移ASP.NET Core2.0

    随着ASP.NET Core 2.0发布之后,原先运行在Windows IIS中的ASP.NET WebApi站点,就可以跨平台运行在Linux中.我们有必要先说一下ASP.NET Core. ASP ...

  3. 【翻译】asp.net core2.0中的token认证

    原文地址:https://developer.okta.com/blog/2018/03/23/token-authentication-aspnetcore-complete-guide token ...

  4. 【转】Asp.Net Core2.0获取客户IP地址,及解决发布到Ubuntu服务器获取不到正确IP解决办法

    1.获取客户端IP地址实现方法(扩展类) using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; u ...

  5. [翻译]在asp.net core2.0 OpenID Connect Handler中丢失了声明(CLaims)?

    注:这是一篇翻译,来自这里.这篇文章讲述了在asp.net core2.0中使用openid connect handler的过程中解析不到你想要的claim时,你可以参考这篇文章. Missing ...

  6. Asp.Net Core2.0获取客户IP地址,及解决发布到Ubuntu服务器获取不到正确IP解决办法

    1.获取客户端IP地址实现方法(扩展类) using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; u ...

  7. VS2017创建一个 ASP.NET Core2.0 应用,并搭建 MVC 框架

    https://testerhome.com/topics/11747 1.使用最新版本的VS2017,并安装.NET Core2.0中相关开发工具   2.打开VS2017,点击文件-新建-项目,选 ...

  8. 在阿里云Windows Server 上部署ASP .NET CORE2.0项目

    近期使用ASP.NET Core2.0对博客进行了重写,在部署到服务器时遇到了一些问题,来记录一下留用. 配置环境 安装 .Net Framework3.5 在IIS管理器上直接开启,这里总是失败,上 ...

  9. 通过Mysql连接ASP.Net Core2.0(Code First模式)

    ASP.NET Core2.0连接Mysql,首先新建项目 选择Web应用程序 选择需要身份验证: 通过Nuget安装Mysql驱动,这里推荐>Pomelo.EntityFrameworkCor ...

随机推荐

  1. [bzoj1707]tanning分配防晒霜_贪心+排序

    tanning分配防晒霜 bzoj-1707 题目大意:给出每个点所能接受的区间,给出m个可以使单个点固定在一个值的方法,每种方法能使用有限次. 注释:1<=N<=2500 想法:这题是瞎 ...

  2. 爬虫(scrapy--豆瓣TOP250)

    # -*- coding: utf-8 -*- import scrapy from douban_top250.items import DoubanTop250Item class MovieSp ...

  3. JavaWeb学习笔记八 监听器

    监听器Listener jservlet规范包括三个技术点:servlet :listener :filter:监听器就是监听某个对象的的状态变化的组件.监听器的相关概念事件源: 被监听的对象(三个域 ...

  4. Alpha第十天

    Alpha第十天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  5. Week03-面向对象入门

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等 类 对象 封装 继承 覆盖 重载 构造函数 static public private toString f ...

  6. 团队作业7——第二次项目冲刺(Beta版本12.05-12.07)

    1.当天站立式会议照片 本次会议内容:1:每个人汇报自己完成的工作.2:组长分配各自要完成的任务. 2.每个人的工作 黄进勇:项目整合,后台代码. 李勇:前台界面优化. 何忠鹏:数据库模块. 郑希彬: ...

  7. ruby:TypeError: 对象不支持此属性或方法

    解决办法. 1.下载对应版本 下载node.js,根据ruby版本决定下载32还是x64,我的ruby版本x64 https://npm.taobao.org/mirrors/node/v8.9.3/ ...

  8. C++高效安全的运行时动态类型转换

    关键字:static_cast,dynamic_cast,fast_dynamic_cast,VS 2015. OS:Window 10. C++类之间类型转换有:static_cast.dynami ...

  9. 从Nest到Nesk -- 模块化Node框架的实践

    文: 达孚(沪江Web前端架构师) 本文原创,转至沪江技术 首先上一下项目地址(:>): Nest:https://github.com/nestjs/nest Nesk:https://git ...

  10. 一、Django的基本用法

    学习Django有一段时间了,整理一下,充当笔记. MVC 大部分开发语言中都有MVC框架 MVC框架的核心思想是:解耦 降低各功能模块之间的耦合性,方便变更,更容易重构代码,最大程度上实现代码的重用 ...