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 81c9687

Browse files
Configuration in asp.net core
1 parent 7704705 commit 81c9687

File tree

7 files changed

+89
-10
lines changed

7 files changed

+89
-10
lines changed

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.Options;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Threading.Tasks;
7-
8+
using Webgentle.BookStore.Models;
9+
using Webgentle.BookStore.Repository;
10+
811
namespace Webgentle.BookStore.Controllers
912
{
1013
public class HomeController : Controller
1114
{
12-
private readonly IConfiguration configuration;
15+
private readonly NewBookAlertConfig _newBookAlertconfiguration;
16+
private readonly IMessageRepository _messageRepository;
1317

14-
public HomeController(IConfiguration_configuration)
18+
public HomeController(IOptionsSnapshot<NewBookAlertConfig>newBookAlertconfiguration,IMessageRepositorymessageRepository)
1519
{
16-
configuration = _configuration;
20+
_newBookAlertconfiguration = newBookAlertconfiguration.Value;
21+
_messageRepository = messageRepository;
1722
}
1823

1924
public ViewResult Index()
2025
{
21-
var result = configuration["AppName"];
22-
var key1 = configuration["infoObj:key1"];
23-
var key2 = configuration["infoObj:key2"];
24-
var key3 = configuration["infoObj:key3:key3obj1"];
26+
bool isDisplay = _newBookAlertconfiguration.DisplayNewBookAlert;
27+
28+
var value = _messageRepository.GetName();
29+
30+
//var newBook = configuration.GetSection("NewBookAlert");
31+
//var result = newBook.GetValue<bool>("DisplayNewBookAlert");
32+
//var bookName = newBook.GetValue<string>("BookName");
33+
34+
//var result = configuration["AppName"];
35+
//var key1 = configuration["infoObj:key1"];
36+
//var key2 = configuration["infoObj:key2"];
37+
//var key3 = configuration["infoObj:key3:key3obj1"];
2538
return View();
2639
}
2740

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Webgentle.BookStore.Models
7+
{
8+
public class NewBookAlertConfig
9+
{
10+
public bool DisplayNewBookAlert { get; set; }
11+
public string BookName { get; set; }
12+
}
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Webgentle.BookStore.Repository
2+
{
3+
public interface IMessageRepository
4+
{
5+
string GetName();
6+
}
7+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.Extensions.Options;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Webgentle.BookStore.Models;
7+
8+
namespace Webgentle.BookStore.Repository
9+
{
10+
public class MessageRepository : IMessageRepository
11+
{
12+
private readonly IOptionsMonitor<NewBookAlertConfig> _newBookAlertconfiguration;
13+
14+
public MessageRepository(IOptionsMonitor<NewBookAlertConfig> newBookAlertconfiguration)
15+
{
16+
_newBookAlertconfiguration = newBookAlertconfiguration;
17+
18+
}
19+
public string GetName()
20+
{
21+
return _newBookAlertconfiguration.CurrentValue.BookName;
22+
}
23+
}
24+
}

‎Webgentle.BookStore/Webgentle.BookStore/Startup.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.Extensions.FileProviders;
1313
using Microsoft.Extensions.Hosting;
1414
using Webgentle.BookStore.Data;
15+
using Webgentle.BookStore.Models;
1516
using Webgentle.BookStore.Repository;
1617

1718
namespace Webgentle.BookStore
@@ -44,6 +45,9 @@ public void ConfigureServices(IServiceCollection services)
4445
#endif
4546
services.AddScoped<IBookRepository, BookRepository>();
4647
services.AddScoped<ILanguageRepository, LanguageRepository>();
48+
services.AddSingleton<IMessageRepository, MessageRepository>();
49+
50+
services.Configure<NewBookAlertConfig>(_configuration.GetSection("NewBookAlert"));
4751
}
4852

4953
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
@inject Microsoft.Extensions.Configuration.IConfiguration _configuration
1+
@using Microsoft.Extensions.Configuration
2+
@inject IConfiguration _configuration
3+
@inject Microsoft.Extensions.Options.IOptionsSnapshot<NewBookAlertConfig> _newBookAlertconfiguration
4+
@{
5+
var newBookAlertconfiguration = _newBookAlertconfiguration.Value;
6+
}
27

38
<header>
49
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
510
<div class="container">
611
<a class="navbar-brand" href="/">
712
<img src="~/images/logo.png" width="30" height="30" asp-append-version="true" />
8-
@_configuration["AppName"]
13+
@_configuration["AppName"]
914
</a>
1015
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
1116
aria-expanded="false" aria-label="Toggle navigation">
@@ -38,4 +43,12 @@
3843
</div>
3944
</div>
4045
</nav>
46+
47+
@if (newBookAlertconfiguration.DisplayNewBookAlert)
48+
{
49+
<div class="alert alert-info" role="alert">
50+
@newBookAlertconfiguration.BookName
51+
</div>
52+
}
53+
4154
</header>

‎Webgentle.BookStore/Webgentle.BookStore/appsettings.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
},
1212
"AllowedHosts": "*",
1313
"AppName": "Book store App",
14+
15+
"NewBookAlert": {
16+
"DisplayNewBookAlert": true,
17+
"BookName": "A new book XYZ-updated.pdf is released"
18+
},
1419
"infoObj": {
1520
"key1": "key 1 value",
1621
"key2": "key 2 value",

0 commit comments

Comments
(0)

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