ASP.NET 4 迁移到 ASP.NET Core 的部分改变
2016-10-17 00:00
302 查看
摘要: 这些改变都是个人在迁移代码时,自己遇到的,如果要看官方的可以跳转到官网连接(见正文)
官方迁移链接:https://docs.asp.net/en/latest/migration/index.html
接下来是正文(会涉及到 .Net Core 的迁移):
从 Type 中获取 Attribute 特性:
原来是这样:
现在是这样:
引用缓存:
原来是这样:
引用 System.Runtime.Caching
定义 ObjectCache cache = MemoryCache.Default; 之后,就可以使用了
现在是这样:
在 project.json 中,依赖 Microsoft.Extensions.Caching.Memory
在Startup.cs 中,添加缓存服务
然后在需要的地方,定义
启用 Session:
原来是这样:
在纯粹的 ASP.NET 应用中,如果 Handler 需要用到 Session,需要实现 IRequiresSessionState 接口
其他的场景我就不知道了,233333,因为没怎么用过,或者太久了给忘了,2333333
现在是这样:
需要注意的是,Session 依赖 Caching,所以除了配置 Session 还要配置 Caching
在 project.json 中,依赖 Microsoft.Extensions.Caching.Memory,Microsoft.AspNet.Session
注册服务
启用 Session
获取项目根路径:
抄自:https://blog.mariusschulz.com/2016/05/22/getting-the-web-root-path-and-the-content-root-path-in-asp-net-core
直接上代码,有什么不明白,看上面的链接
目前就遇到这些,其他的都大同小异吧,所以没印象。
官方迁移链接:https://docs.asp.net/en/latest/migration/index.html
接下来是正文(会涉及到 .Net Core 的迁移):
从 Type 中获取 Attribute 特性:
原来是这样:
type.GetCustomAttributes()
现在是这样:
type.GetTypeInfo().GetCustomAttribute()
引用缓存:
原来是这样:
引用 System.Runtime.Caching
定义 ObjectCache cache = MemoryCache.Default; 之后,就可以使用了
现在是这样:
在 project.json 中,依赖 Microsoft.Extensions.Caching.Memory
在Startup.cs 中,添加缓存服务
public class Startup{ public void ConfigureServices(IServiceCollection services){ services.AddMemoryCache(); } }
然后在需要的地方,定义
IMemoryCache cache = new MemoryCache(new MemoryCacheOptions());
启用 Session:
原来是这样:
在纯粹的 ASP.NET 应用中,如果 Handler 需要用到 Session,需要实现 IRequiresSessionState 接口
其他的场景我就不知道了,233333,因为没怎么用过,或者太久了给忘了,2333333
现在是这样:
需要注意的是,Session 依赖 Caching,所以除了配置 Session 还要配置 Caching
在 project.json 中,依赖 Microsoft.Extensions.Caching.Memory,Microsoft.AspNet.Session
注册服务
public class Startup{ public void ConfigureServices(IServiceCollection services){ services.AddMemoryCache(); services.AddSession(/* options go here */); } }
启用 Session
public class Startup{ public void Configure(IApplicationBuilder app) { app.UseSession(); } }
获取项目根路径:
抄自:https://blog.mariusschulz.com/2016/05/22/getting-the-web-root-path-and-the-content-root-path-in-asp-net-core
直接上代码,有什么不明白,看上面的链接
// Classic ASP.NET public class HomeController : Controller { public ActionResult Index() { string physicalWebRootPath = Server.MapPath("~/"); return Content(physicalWebRootPath); } }
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; namespace AspNetCorePathMapping { public class HomeController : Controller { private readonly IHostingEnvironment _hostingEnvironment; public HomeController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } public ActionResult Index() { string webRootPath = _hostingEnvironment.WebRootPath; string contentRootPath = _hostingEnvironment.ContentRootPath; return Content(webRootPath + "\n" + contentRootPath); } } }
目前就遇到这些,其他的都大同小异吧,所以没印象。
相关文章推荐
- WebApi迁移ASP.NET Core2.0
- 为什么你需要将代码迁移到ASP.NET Core 2.0?
- ASP.NET Core 和 EF Core 系列教程——迁移
- 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移
- Asp.net Core 添加 EF 工具并执行初始迁移错误解决方法(Add-Migration Initial---Build failed.)
- 从博客园博问站点迁移ASP.NET Core展望.NET Core
- 58HouseSearch项目迁移到asp.net core
- 为什么你需要将代码迁移到ASP.NET Core 2.0?
- 从博客园博问站点迁移ASP.NET Core展望.NET Core
- 一起学ASP.NET Core 2.0学习笔记(二)- ef core2.0 及mysql provider 、Fluent API相关配置及迁移
- 向ASP.NET Core迁移
- 为什么你需要将代码迁移到ASP.NET Core 2.0?
- 一步一步带你做WebApi迁移ASP.NET Core2.0
- .NET跨平台之旅:博问站点迁移至ASP.NET Core on Linux并发布上线
- 浅谈ASP.NET Core 2.0 部分视图(译)
- 使用angular4和asp.net core 2 web api做个练习项目(二), 这部分都是angular
- 58HouseSearch项目迁移到asp.net core
- 一步一步带你做WebApi迁移ASP.NET Core2.0
- 为什么你需要将代码迁移到ASP.NET Core 2.0?
- 【ASP.NET Core分布式项目实战】(二)oauth2 + oidc 实现 server部分