質問するログイン新規登録

質問編集履歴

2

「書き換え前のStartsUp」を「書き換えたStartup」に修正しました。

2021/07/06 08:42

投稿

Mk-ky
Mk-ky

スコア19

title CHANGED
File without changes
body CHANGED
@@ -59,7 +59,7 @@
59
59
  }
60
60
  }
61
61
  ```
62
- ### 書き換え前のStartsUp
62
+ ### 書き換えたStartup
63
63
 
64
64
  ```C#
65
65
  using Microsoft.AspNetCore.Builder;

1

変更したstartup.csの内容について追記しました。 使用したテンプレートを追記しました。 ログインページの生成方法を追記しました。

2021/07/06 08:42

投稿

Mk-ky
Mk-ky

スコア19

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  ### 前提
2
2
 
3
3
  .NET5を利用してログインページを作成しました。
4
- そこでログインページを一番最初に表示するためにstartup.csを書き換えたのですがそうするとエラーが表示されました。
4
+ そこでログインページを一番最初に表示するためにHomeControllerにアクションを作成し、startup.csを書き換えた(下記)のですがそうするとエラーが表示されました。
5
5
 
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
@@ -12,15 +12,150 @@
12
12
 
13
13
  ```
14
14
 
15
- ### 該当のソースコード
15
+ ### Controller
16
+ ```C#
17
+ using Microsoft.AspNetCore.Mvc;
18
+ using Microsoft.Extensions.Logging;
19
+ using System;
20
+ using System.Collections.Generic;
21
+ using System.Diagnostics;
22
+ using System.Linq;
23
+ using System.Threading.Tasks;
24
+ using プロジェクト.Models;
16
25
 
26
+ namespace プロジェクト.Controllers
27
+ {
28
+ public class HomeController : Controller
29
+ {
30
+ private readonly ILogger<HomeController> _logger;
31
+
32
+ public HomeController(ILogger<HomeController> logger)
33
+ {
34
+ _logger = logger;
35
+ }
36
+
37
+ public IActionResult Index()
38
+ {
39
+ return View();
40
+ }
41
+
42
+ public IActionResult Privacy()
43
+ {
44
+ return View();
45
+ }
46
+
47
+     //ここから追加
48
+ public IActionResult Login()
49
+ {
50
+ return View("Areas/Identity/Pages/Account/Login.cshtml");
51
+ }
52
+     //ここまで
53
+
54
+ [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
55
+ public IActionResult Error()
56
+ {
57
+ return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
58
+ }
59
+ }
60
+ }
61
+ ```
62
+ ### 書き換え前のStartsUp
63
+
17
64
  ```C#
65
+ using Microsoft.AspNetCore.Builder;
66
+ using Microsoft.AspNetCore.Hosting;
67
+ using Microsoft.AspNetCore.HttpsPolicy;
68
+ using Microsoft.AspNetCore.Identity;
69
+ using Microsoft.AspNetCore.Identity.UI;
70
+ using Microsoft.EntityFrameworkCore;
71
+ using Microsoft.Extensions.Configuration;
72
+ using Microsoft.Extensions.DependencyInjection;
73
+ using Microsoft.Extensions.Hosting;
74
+ using System;
75
+ using System.Collections.Generic;
76
+ using System.Linq;
77
+ using System.Threading.Tasks;
78
+ using プロジェクト.Data;
79
+
80
+ namespace プロジェクト
81
+ {
82
+ public class Startup
83
+ {
84
+ public Startup(IConfiguration configuration)
85
+ {
86
+ Configuration = configuration;
87
+ }
88
+
89
+ public IConfiguration Configuration { get; }
90
+
91
+ // This method gets called by the runtime. Use this method to add services to the container.
92
+ public void ConfigureServices(IServiceCollection services)
93
+ {
94
+ services.AddRazorPages();
95
+
96
+ services.AddDbContext<ApplicationDbContext>(options =>
97
+ options.UseSqlServer(
98
+ Configuration.GetConnectionString("DefaultConnection")));
99
+ services.AddDatabaseDeveloperPageExceptionFilter();
100
+
101
+ services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
102
+ .AddEntityFrameworkStores<ApplicationDbContext>();
103
+ services.AddControllersWithViews();
104
+ }
105
+
106
+ // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
107
+ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
108
+ {
109
+ if (env.IsDevelopment())
110
+ {
111
+ app.UseDeveloperExceptionPage();
112
+ app.UseMigrationsEndPoint();
113
+ }
114
+ else
115
+ {
116
+ app.UseExceptionHandler("/Home/Error");
117
+ // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
118
+ app.UseHsts();
119
+ }
120
+ app.UseHttpsRedirection();
121
+ app.UseStaticFiles();
122
+
123
+ app.UseRouting();
124
+
125
+ app.UseAuthentication();
126
+ app.UseAuthorization();
127
+
128
+ app.UseEndpoints(endpoints =>
129
+ {
130
+ endpoints.MapRazorPages();
131
+ endpoints.MapControllerRoute(
132
+ name: "default",
133
+ pattern: "{controller=Home}/{action=Login}/{id?}"); //ここを書き換えた 
134
+ /*
135
+ 書き換え前は
136
+           pattern: "{controller=Home}/{action=Index}/{id?}");
137
+ */
138
+
139
+ });
140
+ }
141
+ }
142
+ }
143
+ ```
144
+
145
+
146
+
147
+
148
+
149
+
150
+ ### Login.cshtml
151
+
152
+ ```Razor
18
153
  @page
19
154
 
20
155
  @model LoginModel
21
156
 
22
157
  @{
23
- ViewData["Title"] = "ログイン"; @*ここにNullReferenceException: Object reference not set to an instance of an object.が表示されます*@
158
+ ViewData["Title"] = "ログイン"; //@*ここにNullReferenceException: Object reference not set to an instance of an object.が表示されます*@
24
159
  }
25
160
 
26
161
  <h1> ログイン </h1> 
@@ -64,7 +199,7 @@
64
199
 
65
200
 
66
201
  ### 補足情報
202
+ VS2019のテンプレートはRazorの認証に「個別のアカウント」を使ってプロジェクトを生成し、Loginページをスキャフォールディング機能を使って作成しました。
67
203
 
68
-
69
204
  Microsoft Visual Studio Community 2019 Version 16.9.4
70
205
  .NET5 MVC