最新在将原来写的一些webSerivce转换为WebApi,直接就用了ASP.Net Core 2.0的框架,在使用中,发现的与原有的asp.net不一样的地方,经过搜索已经慢慢解决,记录下来备用。web
1、全局配置数据库
在asp.net中,全局变动配置写在web.config中,以下所示json
1 <?xml version="1.0"?> 2 <configuration> 3 <connectionStrings> 4 <add name="conn" connectionString="Data Source=localhost;Initial Catalog=helloworld;Integrated Security=True"/> 5 </connectionStrings> 6 <appSettings> 7 <add key="app_key" value="helloworld" /> 8 <add key="app_secret" value="1234567890abcdef" /> 9 </appSettings> 10 </configuration>
在ASP.Net Core 2.0 WebApi中,已经没有了web.config文件,查了一些资料,能够把全局变量配置写在appsetting.json文件中,以下所示:app
{ "connectionStrings": { "conn": "Data Source=localhost;Initial Catalog=helloworld;Integrated Security=True" } "appSettings": { "app_key": "helloworld", "app_secret": "1234567890abcdef" } }
这样一来,在程序中就能够对全局变量配置进行引用了。框架
使用appSetting.json,全局变量能够设置的更为复杂,具体的方法能够参考文后的参考文献。asp.net
2、记录日志函数
之前ASP.NET的时候,日志都是用Nlog进行记录,如今转换到了Core 2.0,也准备继续使用Nlog,在使用中,发现和之前的有也所不一样。ui
首先,在Nuget中获取NLog.Web.AspNetCore包,spa
而后将startup.cs文件的代码进行修改.net
public void Configure(IApplicationBuilder app, IHostingEnvironment env) //修改成 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
并在Configure函数中,加上如下语句:
loggerFactory.AddNLog();
app.AddNLogWeb();
loggerFactory.ConfigureNLog(“nlog.config”);
记得要在文件头先引用using NLog.Web和using NLog.Extensions.Logging;
增长一个"Web配置文件",文件名为nlog.config,内容以下:
<?xml version="1.0" encoding="utf-8"?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets> <target xsi:type="File" name="logfile" fileName="${basedir}/logs/${shortdate}.log" keepFileOpen="false" layout="${longdate}|${callsite:fileName=True}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="debugfile" fileName="${basedir}/logs/${shortdate}_debug.log" keepFileOpen="false" layout="${longdate}|${callsite:fileName=True}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="errfile" fileName="${basedir}/logs/${shortdate}_error.log" keepFileOpen="false" layout="${longdate}|${callsite:fileName=True}|${uppercase:${level}}|${message} ${exception}" /> </targets> <rules> <logger name="*" level="Debug" writeTo="debugfile" /> <logger name="*" level="Error" writeTo="errfile" /> <logger name="*" minlevel="Trace" writeTo="logfile" /> </rules> </nlog>
而后在程序中就能够开始调用日志功能了。
二个功能的DEMO代码以下:
using System; using System.IO; using Microsoft.Extensions.Configuration; using NLog.Extensions.Logging; using NLog.Web; public class Program { public static IConfigurationRoot Configuration { get; set; } public static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger(); public static void ConfigAndLog() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); string app_key = Configuration["appSettings:app_key"]; string coon = Configuration["connectionStrings:conn"]; log.Debug("数据库链接为:" + conn); return; } }
参考文档: