升级完类库项目,第二篇,我们来升级ASP.Net Core项目
修改global.json与project.json
这里可以参照,升级.Net Core RC2的那些事(一)
这里补充一点就是如果你觉得这样修改复杂,你完全可以新建一个项目,把这两个文件拷贝过来,再加上自己引用过的包,也是可以的,看你觉得那种方法更简单点
增加Program入口类
Program.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Program { public static void Main( string [] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); } } |
并移除Startup.cs中的
1 2 | // Entry point for the application. public static void Main( string [] args) => WebApplication.Run<Startup>(args); |
修改类名和命名空间名
将所有 Microsoft.AspNet.* 的命名空间修改为 Microsoft.AspNetCore.*
参照下表修改对应的类名
旧类 | 新类 |
IWebApplicationBuilder | IWebHostBuilder |
WebApplicationBuilder | WebHostBuilder |
IWebApplication | IWebHost |
WebApplication | WebHost |
WebApplicationOptions | WebHostOptions |
WebApplicationDefaults | WebHostDefaults |
WebApplicationService | WebHostService |
WebApplicationConfiguration | WebHostConfiguration |
如果你有使用全局环境变量,也请对照下表修改
Old prefix | New prefix |
---|---|
ASPNET_WEBROOT | ASPNETCORE_WEBROOT |
ASPNET_SERVER | ASPNETCORE_SERVER |
ASPNET_APP | ASPNETCORE_APPLICATIONNAME |
ASPNET_ENVIRONMENT | ASPNETCORE_ENVIRONMENT |
ASPNET_DETAILEDERRORS | ASPNETCORE_DETAILEDERRORS |
修改Logging
如果你有使用日志功能,需要修改下日志等级的配置,具体在 appsettings.json
例如:
1 2 3 4 5 6 7 8 | "Logging" : { "IncludeScopes" : false , "LogLevel" : { "Default" : "Debug" , "System" : "Information" , "Microsoft" : "Information" } }, |
具体请对照下表:
Old Levels | New Levels |
---|---|
Critical | Critical |
Error | Error |
Warning | Warning |
Information | Information |
Verbose | Debug |
Debug | Trace |
关于PostAsJsonAsync与ReadAsAsync
如果以前有使用 Microsoft.AspNet.WebApi.Client 的,当然现在也是可以直接引用这个包来实现Web API之间的对接的
但我在实战中发现,也许是因为引用包之间的版本冲突,会有诡异的异常,于是我自己实现了一个Helper也分享给大家
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public static class HttpClientHelper { public static async Task<T> ReadAsAsync<T>( this HttpContent content) { return JsonConvert.DeserializeObject<T>(await content.ReadAsStringAsync()); } public static async Task<HttpResponseMessage> PostAsJsonAsync<T>( this HttpClient client, string url, T model) { SetHeader(client); return await client.PostAsync(url, SetContent(model)); } private static StringContent SetContent<T>(T model) { return new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json" ); } private static void SetHeader(HttpClient client) { client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue( "application/json" )); client.DefaultRequestHeaders.AcceptCharset.Add( new StringWithQualityHeaderValue( "utf-8" )); } public static async Task<HttpResponseMessage> PutAsJsonAsync<T>( this HttpClient client, string url, T model) { SetHeader(client); return await client.PutAsync(url, SetContent(model)); } } |
关于文件上传保存
以前有.SaveAsAsync,这样便捷的扩展方法保存的,现在没有了(或者说藏到了哪里目前找不到了),于是就需要手写
相关的代码片段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | if (model.picdata != null ) { var extName = ContentDispositionHeaderValue.Parse(model.picdata.ContentDisposition).FileName.Trim( '"' ); int i = extName.LastIndexOf( '.' ); extName = extName.Substring(i); string fileName = Guid.NewGuid() + extName; var filePath = _hostingEnvironment.WebRootPath + @"\upload\" + fileName; //保存文件 using ( var fileStream = new FileStream(filePath, FileMode.Create)) { var inputStream = model.picdata.OpenReadStream(); await inputStream.CopyToAsync(fileStream); } //await model.picdata.SaveAsAsync(filePath); } |
本人的项目中就这一处需要保存文件的,就不封装了:),你喜欢也可以自己封装下
关于System.Drawing
由于项目中需要使用图片处理(生成二维码),好多大神建议另外建项目调用,但我想做在一个项目中,故有此一段
首先,ASP.Net Core项目是不支持System.Drawing的,如果需要使用,则需要修改成纯 net461 的项目,这里需要修改一下project.json
1、删除 Microsoft.NETCore.App 配置节
1 2 3 4 | "Microsoft.NETCore.App" : { "version" : "1.0.0-rc2-3002702" , "type" : "platform" }, |
2、修改 frameworks 配置节,只保留 net461 项
1 2 3 4 5 6 7 8 9 10 11 | "frameworks" : { "net461" : { "frameworkAssemblies" : { "System.Drawing" : "4.0.0.0" }, "dependencies" : { "ThoughtWorks.QRCode" : "1.1.0" , "Microsoft.NETCore.Platforms" : "1.0.1-rc2-24027" } } }, |
这样修改完成后就可以在项目中使用 System.Drawing 命名空间了
副作用:
这样修改后,项目是不能在CoreCLR上跑的,Windows环境下不会存在问题,Linux下则需要配置Mono环境才可以
关于Session
现在光在 ConfigureServices 方法中加 services.AddSession(); 是不够的,实际使用时会报错
还需要在 Configure 方法中加 app.UseSession();
发布到IIS
通过VS发布
现在你可以像MVC5那样通过 右键菜单的 发布 功能,用Web Deploy来发布到IIS,需要注意的是目前这个工具对中文的支持不是很好,所以不要使用中文的项目名哟
通过cli发布
你也可以通过以下命令获得发布文件
1 | dotnet publish -c release |
然后把获得的文件通过FTP等途径上传到服务器
PS:此命令需要在 project.json 的同级目录下运行
IIS配置
IIS绑定的目录应该是最多DLL的那一层,而不是以前RC1的wwwroot
应用程序池中设置 无托管代码 等,这些基本和RC1一致
其他补充
如果你的项目存在Areas,默认是不会发布出去的,此时需修改project.json的publishOptions配置节,例如:
1 2 3 4 5 6 7 8 9 | "publishOptions" : { "include" : [ "wwwroot" , "Views" , "Areas/Admin/Views" , "appsettings.json" , "web.config" ] }, |
相关文章:
ASP.NET Core 1.0 入门——了解一个空项目
ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1)
.NET Core 1.0、ASP.NET Core 1.0和EF Core 1.0简介
云服务器下ASP.NET Core 1.0环境搭建(包含mono与coreclr)
使用VS Code开发ASP.NET Core 应用程序
dotnet run是如何启动asp.net core站点的
ASP.NET Core提供模块化Middleware组件
“dotnet restore"和"dotnet run"都做了些什么?
探秘 dotnet run 如何运行 .NET Core 应用程序
.NET Portability Analyzer 已开源
ASP.NET Core的配置(2):配置模型详解
.NET Core 1.0 RC2 历险之旅
使用VS Code开发 调试.NET Core 应用程序
让我们Core在一起:ASP.NET Core & .NET Core
.NET Core VS Code 环境配置
官方博客明确了 .NET Core RC2/RTM 时间表
.NET Core全新的配置管理[共9篇]
利用记事本创建一个ASP.NET Core RC2 MVC应用
微软.NET 正式劈腿成功,横跨所有平台
.NET Core 1.0 CentOS7 尝试
解读发布:.NET Core RC2 and .NET Core SDK Preview 1
[.NET Core].NET Core R2安装及示例教程
原文地址:http://blog.lishewen.com/post/to-upgrade-thenet-core-rc2-(2)-those-things-to-upgrade-the-core-program-at-aspnet
.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注