Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1d5b563

Browse files
Appsettings file in asp.net core
1 parent 9328ba0 commit 1d5b563

File tree

8 files changed

+120
-77
lines changed

8 files changed

+120
-77
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Controllers/HomeController.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.Extensions.Configuration;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -7,9 +8,20 @@
78
namespace Webgentle.BookStore.Controllers
89
{
910
public class HomeController : Controller
10-
{
11+
{
12+
private readonly IConfiguration configuration;
13+
14+
public HomeController(IConfiguration _configuration)
15+
{
16+
configuration = _configuration;
17+
}
18+
1119
public ViewResult Index()
1220
{
21+
var result = configuration["AppName"];
22+
var key1 = configuration["infoObj:key1"];
23+
var key2 = configuration["infoObj:key2"];
24+
var key3 = configuration["infoObj:key3:key3obj1"];
1325
return View();
1426
}
1527

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Hosting;
6-
using Microsoft.Extensions.Configuration;
7-
using Microsoft.Extensions.Hosting;
8-
using Microsoft.Extensions.Logging;
9-
10-
namespace Webgentle.BookStore
11-
{
12-
public class Program
13-
{
14-
public static void Main(string[] args)
15-
{
16-
CreateHostBuilder(args).Build().Run();
17-
}
18-
19-
public static IHostBuilder CreateHostBuilder(string[] args) =>
20-
Host.CreateDefaultBuilder(args)
21-
.ConfigureWebHostDefaults(webBuilder =>
22-
{
23-
webBuilder.UseStartup<Startup>();
24-
});
25-
}
26-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace Webgentle.BookStore
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
{
2-
"iisSettings": {
3-
"windowsAuthentication": false,
4-
"anonymousAuthentication": true,
5-
"iisExpress": {
6-
"applicationUrl": "http://localhost:65352",
7-
"sslPort": 0
8-
}
9-
},
10-
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
13-
"launchBrowser": true,
14-
"environmentVariables": {
15-
"ASPNETCORE_ENVIRONMENT": "Development"
16-
}
17-
},
18-
"Webgentle.BookStore": {
19-
"commandName": "Project",
20-
"launchBrowser": true,
21-
"applicationUrl": "http://localhost:5000",
22-
"environmentVariables": {
23-
"ASPNETCORE_ENVIRONMENT": "Development"
24-
}
25-
}
26-
}
27-
}
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:65352",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Production"
16+
}
17+
},
18+
"Webgentle.BookStore": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"applicationUrl": "http://localhost:5000",
22+
"environmentVariables": {
23+
"ASPNETCORE_ENVIRONMENT": "Development"
24+
}
25+
}
26+
}
27+
}

‎Webgentle.BookStore/Webgentle.BookStore/Repository/BookRepository.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Configuration;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -11,10 +12,12 @@ namespace Webgentle.BookStore.Repository
1112
public class BookRepository : IBookRepository
1213
{
1314
private readonly BookStoreContext _context = null;
15+
private readonly IConfiguration _configuration;
1416

15-
public BookRepository(BookStoreContext context)
17+
public BookRepository(BookStoreContext context,IConfigurationconfiguration)
1618
{
1719
_context = context;
20+
_configuration = configuration;
1821
}
1922

2023
public async Task<int> AddNewBook(BookModel model)
@@ -115,7 +118,7 @@ public List<BookModel> SearchBook(string title, string authorName)
115118

116119
public string GetAppName()
117120
{
118-
return "Book store application";
121+
return _configuration["AppName"];
119122
}
120123
}
121124
}

‎Webgentle.BookStore/Webgentle.BookStore/Views/Shared/_header.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
@inject Webgentle.BookStore.Repository.IBookRepository_bookRepo
1+
@inject Microsoft.Extensions.Configuration.IConfiguration_configuration
22

33
<header>
44
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
55
<div class="container">
66
<a class="navbar-brand" href="/">
77
<img src="~/images/logo.png" width="30" height="30" asp-append-version="true" />
8-
@_bookRepo.GetAppName()
8+
@_configuration["AppName"]
99
</a>
1010
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
1111
aria-expanded="false" aria-label="Toggle navigation">
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Information",
5-
"Microsoft": "Warning",
6-
"Microsoft.Hosting.Lifetime": "Information"
7-
}
8-
}
9-
}
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AppName": "Book store from development json file",
10+
"infoObj": {
11+
"key1": "key 1 value d",
12+
"key2": "key 2 value d",
13+
"key3": {
14+
"key3obj1": "key 3 obj 1 value d"
15+
}
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"ConnectionStrings": {
3+
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true"
4+
},
5+
"infoObj": {
6+
"key1": "key 1 value p",
7+
"key2": "key 2 value p",
8+
"key3": {
9+
"key3obj1": "key 3 obj 1 value p"
10+
}
11+
}
12+
}
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
{
2-
"Logging": {
3-
"LogLevel": {
4-
"Default": "Information",
5-
"Microsoft": "Warning",
6-
"Microsoft.Hosting.Lifetime": "Information"
7-
}
8-
},
9-
"AllowedHosts": "*"
10-
}
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*",
10+
"AppName": "Book store App",
11+
"infoObj": {
12+
"key1": "key 1 value",
13+
"key2": "key 2 value",
14+
"key3": {
15+
"key3obj1": "key 3 obj 1 value"
16+
}
17+
}
18+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /