我的问题
现在,当我运行 dotnet 构建时,我在网页上看不到任何内容。在 chrome 上我得到:
This site can’t provide a secure connectionlocalhost sent an invalid response.
ERR_SSL_PROTOCOL_ERROR
在 Safari 上我可以看到一个普通的网站。
设置 我得到了 Visual Studio 代码和 .Net 6.0.421。
项目结构
-Controllers--HomeController.cs
-TeachingPlanController.cs
-Modells--ErrorViewModell.cs
-Lesson.cs
-LessonViewModell.cs
-TeachingPlanViewModell.cs
-Properties
-Viewes--Home--Index.cshtml
-Shared
-TeachingPlan--Index.cshtml
-appsetting.Development.json
-appsetting.json
-Programm.cs
-SoPro24Team01.csproj
在某些目录中还有一些文件,但我认为它们不是必需的。当您需要所有文件时,请发表评论:)
我的代码
教学计划控制器.cs:
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using SoPro24Team01.Models;
namespace SoPro24Team01.Controllers
{
[Route("[controller]")]
public class TeachingPlanController : Controller
{
public ViewResult Index()
{
var lessons = new List<Lesson>
{
new Lesson(1, "Introduction to Programming", "http://example.com/deck1", TimeSpan.FromHours(1)),
new Lesson(2, "Data Structures and Algorithms", "http://example.com/deck2", TimeSpan.FromHours(2)),
new Lesson(3, "Web Development Basics", "http://example.com/deck3", TimeSpan.FromHours(3))
};
var lessonViewModels = lessons.Select(lesson => new LessonViewModel
{
Lesson = lesson
}).ToList();
var viewModel = new TeachingPlanViewModel
{
Lessons = lessonViewModels
};
return View(viewModel);
}
}
}
Lesson.cs:
命名空间 SoPro24Team01.Models { 公开课
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private string? name;
public string? Name
{
get { return name; }
set { name = value; }
}
private string? cardDeckLink;
public string? CardDeckLink
{
get { return cardDeckLink; }
set { cardDeckLink = value; }
}
private TimeSpan timeEstimation;
public TimeSpan TimeEstimation
{
get { return timeEstimation; }
set { timeEstimation = value; }
}
public Lesson(int id, string name, string cardDeckLink, TimeSpan timeEstimation)
{
Id = id;
Name = name;
CardDeckLink = cardDeckLink;
TimeEstimation = timeEstimation;
}
}
} LessonViewModel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SoPro24Team01.Models
{
public class LessonViewModel
{
public Lesson? Lesson { get; set; }
}
}
教学计划视图模型.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SoPro24Team01.Models
{
public class TeachingPlanViewModel
{
public List<LessonViewModel>? Lessons { get; set; }
public string? Title { get; set; }
public string? Header { get; set; }
}
}
现在我的索引(在/views/TeachingPlan)页面:
@model SoPro24Team01.Models.TeachingPlanViewModel
<html>
<head>
</head>
<body>
<p>This is a test</p>
</body>
</html>
稍后我想打印页面上的所有课程,但这不是我的问题的一部分。
programm.cs:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=TeachingPlan}/{action=Index}/{id?}");
app.Run();
我将此文件中的controller=Home更改为controller=TeachingPlan,但这没有帮助。我不知道出了什么问题。
它看起来像是基于 SSL 的问题,很可能可以通过在 cmd 编辑器中运行以下命令来解决此问题。您也可以查看这个指南
dotnet dev-certs https --trust
另一个解决方案是更改
launchSettings.json
文件中应用程序的起始 url,删除 https 并使用 http 代替