【T9a】簡易ブログソフトウェアの作成 Part.Ⅰ ~ 認証機能の組み込み(4/14) プロジェクトタイプ ASP.NET Core Web アプリ(Model-View-Controller) プロジェクト名 T9a
ソリューション名 PIT9
ターゲットフレームワーク .NET 8.0(長期的なサポート) 最上位レベルのステートメントを使用しない 使用する(チェックオフ)
9a-4. 準備作業 まずは以降の作業で使用するプロジェクトを作成しよう.
上表 に示すように,「ASP.NET Core Web アプリ(Model-View-Controller) 」タイプのプロジェクトT9a
を,
ソリューション PIT9 に追加する.ターゲットフレームワークとして「.NET 8.0(長期的なサポート)」を選択し,
「HTTPS用の構成」のチェックを外すこと,また「最上位レベルのステートメントを使用しない」にチェックが入っていない ことを確認しよう.
プロジェクトを作成したら以下の準備作業を行う.順を追って実施しよう.
【作業Ⅰ】 作業対象:プロジェクト T9a【作業Ⅱ】 作業対象: Views/Shared/_Layout.cshtml (ファイル)【作業Ⅲ】 作業対象: wwwroot/css/site.css (ファイル)【作業Ⅳ】 作業対象:Home
コントローラー&ビュー【作業Ⅴ】 作業対象: Program.cs (ファイル)【作業Ⅰ】
【作業Ⅰ】 NuGetを使って以下のパッケージを探してプロジェクトにインストールする.
パッケージソース パッケージ名 本校執筆時 の安定版 nuget.org Microsoft.EntityFrameworkCore.Design
8.0.3 〃 Npgsql.EntityFrameworkCore.PostgreSQL
8.0.2 〃 Npgsql.EntityFrameworkCore.PostgreSQL.Design
1.1.0 〃 Microsoft.AspNetCore.Identity.UI
8.0.3 〃 Microsoft.AspNetCore.Identity.EntityFrameworkCore
8.0.3 knzw.tech KnzwTech.AspNetCore.ResourceBasedLocalization.ja
6.0.1
【作業Ⅰ】 : 閉じる
【作業Ⅱ】
【作業Ⅱ】 チュートリアル【T4a】 を参考に以下のことを行う.
Views/Shared/_Layout.cshtml をリスト4a-4-2 に示すように変更する. ↑の5行目の T4a
を T9a
に変更し,@* サイトタイトル *@
の部分を チュートリアルT9a
に変更する. 【作業Ⅱ】 : 閉じる
【作業Ⅲ】
【作業Ⅲ】 wwwroot/css/site.css の末尾に_ を追記する.
wwwroot/css/site.cssの追記内容 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
body { padding : 1 em ; }
table {
border-collapse : collapse ;
}
th , td {
border : 1 px solid black ;
}
th {
background : #404040 ;
color : white ;
text-align : center ;
font-weight : bold ;
}
section . article {
border : 1 px solid lightgray ;
border-radius : 10 px ;
background : #fafaff ;
padding : 1 em ;
margin : 1 em ;
}
section . article pre , pre . article {
min-width : 8 em ;
min-height : 6 em ;
border : 2 px dotted lightgray ;
background : white ;
font-family : monospace ;
padding : 1 em ;
font-size : large ;
}
input [ type = "text" ][ name = "Title" ] {
width : 30 em ;
}
. table th
{
color : inherit ;
}
img . rep {
max-width : 100 % ;
}
【作業Ⅲ】 : 閉じる
【作業Ⅳ】
【作業Ⅳ】 チュートリアル【T4b】 を参考に以下のことを行う.
Home
コントローラーにリスト4b-6-3 のアクションメソッドAccessError
を追記する.プロジェクト内のViews
フォルダのHome
フォルダに新規にAccessError
(.cshtml)という名前のビューを追加し,リスト4b-6-4 の内容を書き込む 【作業Ⅳ】 : 閉じる
【作業Ⅴ】
【作業Ⅴ】 Program.cs に_ に示す内容を追記する.
Program.csの変更内容 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using KnzwTech.AspNetCore.ResourceBasedLocalization ; // 追記
AppContext . SetSwitch ( "Npgsql.EnableLegacyTimestampBehavior" , true ); // 追記
var builder = WebApplication . CreateBuilder ( args );
// Add services to the container.
builder . Services . AddControllersWithViews ( opt => opt . EnableDefaultErrorMessagesFromResource ()); // 追記
var app = builder . Build ();
// Configure the HTTP request pipeline.
if (! app . Environment . IsDevelopment ())
{
app . UseExceptionHandler ( "/Home/Error" );
}
app . UseStaticFiles ();
app . UseStatusCodePagesWithReExecute ( "/Home/AccessError/{0}" ); // 追記
app . UseRouting ();
app . UseAuthorization ();
app . MapControllerRoute (
name : "default" ,
pattern : "{controller=Home}/{action=Index}/{id?}" );
app . Run ();
【作業Ⅴ】 : 閉じる
以上の編集を終えたうえで次に進もう.
Last updated on 2024-06-19 Published on 2024-06-19