Tuesday, August 2, 2016
Now that we have most of our static contents taken care of for our application, meaning we did everything we could without a database. It's time to create our database. But before we can do that we need a place to store our connection string to the database. Usually we just store the connection string in the web.config file in our web application. However, since ASP.NET Core is trying to break free from the old way of doing things, there's a new way to store configuration information which is more flexible the old web.config way. As usual it also requires more work.
To setup a new .json configuration file perform the following steps:
1. Create a new file call "appSettings.json"
2. Type in the following markup in the appSettings.json file
The "AppSettings", and "Data" sections are the root section of the configuration. So if you want to access the "NorthwindContextConnection" you would type ["Data": "NorthwindContextConnection"] you can go as many levels deep as you want.
3. Now we need to setup the Startup.cs file to recognize the new appSettings.json configuration file
at the top of the Startup class declare a static variable call Configuration
public static IConfigurationRoot Configuration;
The IConfigurationRoot is in the Microsoft.Extensions.Configuration
4. Now create Startup constructor to add the appSettings.json file, like the code below
The adds the appSettings.json file in the root directory of the application and then builds the key/values information into the Configuration object.
Note:
For the ".SetBasePath" method you will have add the "Microsoft.Extensions.Configuration.FileExtensions" package
For the ".AddJsonFile" method you will have to add the "Microsoft.Extensions.Configuration.Json" package
5. Now go to the ConfigureServices method and right below the services.AddMvc() method call, type in the following
var connectionString = Startup.Configuration["Data:NorthwindContextConnection"];
The complete method should look like the following:
If you put a break point where the connectionString line is you will see that the connection in the appSettings.json file has been retrieved
To setup a new .json configuration file perform the following steps:
1. Create a new file call "appSettings.json"
2. Type in the following markup in the appSettings.json file
{ "AppSettings": { }, "Data": { "NorthwindContextConnection": "Server=yourservername;Database=NorthwindCafe;Integrated Security=True;MultipleActiveResultSets=true" } }
The "AppSettings", and "Data" sections are the root section of the configuration. So if you want to access the "NorthwindContextConnection" you would type ["Data": "NorthwindContextConnection"] you can go as many levels deep as you want.
3. Now we need to setup the Startup.cs file to recognize the new appSettings.json configuration file
at the top of the Startup class declare a static variable call Configuration
public static IConfigurationRoot Configuration;
The IConfigurationRoot is in the Microsoft.Extensions.Configuration
4. Now create Startup constructor to add the appSettings.json file, like the code below
public static IConfigurationRoot Configuration { get; private set; } public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appSettings.json") .AddEnvironmentVariables(); Configuration = builder.Build(); }
The adds the appSettings.json file in the root directory of the application and then builds the key/values information into the Configuration object.
Note:
For the ".SetBasePath" method you will have add the "Microsoft.Extensions.Configuration.FileExtensions" package
For the ".AddJsonFile" method you will have to add the "Microsoft.Extensions.Configuration.Json" package
5. Now go to the ConfigureServices method and right below the services.AddMvc() method call, type in the following
var connectionString = Startup.Configuration["Data:NorthwindContextConnection"];
The complete method should look like the following:
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); var connectionString = Startup.Configuration["Data:NorthwindContextConnection"]; }
If you put a break point where the connectionString line is you will see that the connection in the appSettings.json file has been retrieved
ASP.NET Core Posts:
- How To Create An ASP.NET Core Application From Scratch
- ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
- Enable MVC On ASP.NET Core Application
- ASP.NET Core : Create Our First Controller and View
- ASP.NET Core : Adding The Default View With _ViewStart.cshtml
- ASP.NET Core : Create A Responsive Layout With Bootstrap
- ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons
- ASP.NET Core : Add .json Configuration Files With Microsoft.Extensions.Configuration Library
- ASP.NET Core : Entity Framework Core Models For Northwind Cafe
- ASP.NET Core : Create The NothwindContext ( EntityFrameworkCore )
- ASP.NET Core : Configure project.json File To Support Entity Framework Core
- ASP.NET Core : Add NorthwindContext To Startup Class And Create Database
- ASP.NET Core: Seeding The NorwindCafe Database
- ASP.NET Core: Add Logging To The NorthwindCafe Application
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Nice article. I like your blog. Its really Informative Thanks you so much for sharing.
ReplyDeleteonline internship
online internships
watch internship online
online internship for students
the internship online
online internship with certificate
online internship certificate
python online internship
data science online internship
Liên hệ Aivivu, đặt vé máy bay tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu tiền
vé máy bay từ seattle về việt nam
khi nào có chuyến bay từ canada về việt nam
giá vé máy bay từ nhật về việt nam
Máy bay từ Hàn Quốc về Việt Nam
Vé máy bay từ Đài Loan về Việt Nam
vé máy bay chuyên gia nước ngoài
I am a student and it's strange when others tell me that students are very overtired, they just couldn't find their assistant, and I just know one secret that helps me cope with the essay, this is grademiners, they will always write a high-quality and competent essay that the teacher will like
ReplyDelete