ASP.NET CORE MVC 开发常用代码备份
1. 在页面中进行注入配置文件操作
注册对象
class Program { static void Main() { Host.CreateDefaultBuilder().ConfigureWebHostDefaults(builder => builder .ConfigureServices(svcs => svcs .AddSingleton<IFoobar, Foobar>() .AddControllersWithViews()) .Configure(app => app .UseRouting() .UseEndpoints(endpoints => endpoints.MapControllers()))) .Build() .Run(); }
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(_configuration.GetSection("AppSettings")); //注入配置文件
}
}
View视图使用获取注入对象
@inject IFoobar Foobar ; //接口的方式
@inject Microsoft.Extensions.Options.IOptions<Saas.LCI.Admin.Common.AppSettings>
_appSettingsOptions
; //配置文件注入获取
@ { Debug.Assert(Foobar!= null);
var _appSettings = _appSettingsOptions.Value;
}