对不起,有';当我用C#Blazor点击链接时,这个地址页面上没有显示任何内容

对不起,有';当我用C#Blazor点击链接时,这个地址页面上没有显示任何内容,c#,blazor,asp.net-blazor,C#,Blazor,Asp.net Blazor,我正试着跟着youtube tuturial学习Blazor。我想用下面的代码显示学生名单。当我点击学生链接时,我说“对不起,这个地址什么都没有。”请问有人能告诉我哪里出了问题吗 导航栏 <li class="nav-item px-3"> <NavLink class="nav-link" href="Pages/Student"> <span class="oi oi-list-rich" aria-hidden=

我正试着跟着youtube tuturial学习Blazor。我想用下面的代码显示学生名单。当我点击学生链接时,我说“对不起,这个地址什么都没有。”请问有人能告诉我哪里出了问题吗

导航栏

    <li class="nav-item px-3">
        <NavLink class="nav-link" href="Pages/Student">
            <span class="oi oi-list-rich" aria-hidden="true"></span> Student data
        </NavLink>
    </li>
  • 学生数据
  • 学生控制员

        public class StudentController : ControllerBase
        {
        private readonly RazorExampleContext _context;
    
        public StudentController(RazorExampleContext context)
        {
            _context = context;
        }
        // GET: api/Student
        [HttpGet]
        public async Task<ActionResult<List<Student>>> GetStudent()
        {
            return await _context.Student.ToListAsync();
        }
       }
    
    public class学生控制器:ControllerBase
    {
    私有只读RazoreExampleContext\u上下文;
    公共学生控制器(RazoreExampleContext上下文)
    {
    _上下文=上下文;
    }
    //获取:api/学生
    [HttpGet]
    公共异步任务
    删除
    学生证
    student.StudentName
    }
    }
    @代码{
    学生[]学生{get;set;}
    受保护的重写异步任务OnInitializedAsync()
    {
    等待加载学生();
    }
    异步任务LoadStudent()
    {
    students=wait Http.GetJsonAsync(“api/Student”);
    }
    }
    
    Startup.cs

     // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application
     public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddServerSideBlazor();
            services.AddHttpClient();
            services.AddSingleton<WeatherForecastService>();
            services.AddSingleton<Student>();
        }
    
    //此方法由运行时调用。使用此方法向容器中添加服务。
    //有关如何配置应用程序的详细信息,请参见
    public void配置服务(IServiceCollection服务)
    {
    services.AddRazorPages();
    AddServerSideBlazor();
    services.AddHttpClient();
    services.AddSingleton();
    services.AddSingleton();
    }
    
    当路由器找不到提供给它的url地址时,通常会发生此错误。更改此项:

    与答案无关:

    • 为什么这样做:
      services.AddSingleton()学生是您的模型,而不是要注入组件的服务

    • 在foreach循环中使用Student数组之前,应验证该数组是否已填充数据,否则将触发空引用异常

    • 按照惯例,路线模板应为@page”/student,而不是@page”/student


    • href=“/Pages/Student”>
      @page”/Pages/Student“
      。顺便说一句,了解@dani不明白您想让我做什么,请回答问题我做了您要求的更改,但现在我遇到错误“InvalidOperationException:无法为'StudentProject.Pages.Student.Student'类型的属性'Http'提供值。没有'System.Net.Http.HttpClient'类型的注册服务。”在我的例子中,问题是由于某种奇怪的原因,by.blazor文件将构建操作设置为“None”而不是“Content”。我把它改成了“内容”,它成功了
       // This method gets called by the runtime. Use this method to add services to the container.
          // For more information on how to configure your application
       public void ConfigureServices(IServiceCollection services)
          {
              services.AddRazorPages();
              services.AddServerSideBlazor();
              services.AddHttpClient();
              services.AddSingleton<WeatherForecastService>();
              services.AddSingleton<Student>();
          }