C# ASP.NET Core 3.1中的AJAX请求。剃刀

C# ASP.NET Core 3.1中的AJAX请求。剃刀,c#,asp.net-core,razor-pages,C#,Asp.net Core,Razor Pages,我试图遵循以下指南: 当我尝试单击按钮执行AJAX get请求时,出现Http 500错误: System.InvalidOperationException:尚未注册类型为“RazorPagesAJAX.Interfaces.CarService”的服务 我很确定我已经用代码注册了CarService: public void ConfigureServices(IServiceCollection services) { services.AddTransient<ICarS

我试图遵循以下指南:

当我尝试单击按钮执行AJAX get请求时,出现Http 500错误:

System.InvalidOperationException:尚未注册类型为“RazorPagesAJAX.Interfaces.CarService”的服务

我很确定我已经用代码注册了
CarService

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<ICarService, CarService>();
    services.AddRazorPages();
}
也许这就是问题所在

我无法从指南中获取代码

 @model List<Car>
@型号列表
工作

任何帮助都将不胜感激:)

我的代码:

Index.cshtml:

@page
@model RazorPagesAJAX.Pages.IndexModel

<divclass="text-center">
    <h1 class="display-4">Razor Pages AJAX Example</h1>
    <p>Main page updated at - @DateTime.Now.</p>
</div>

<h2>Ajax</h2>
<p><button class="btn btn-primary" id="load">Load</button></p>
<div id="grid"></div>
@section scripts{
    <script>
        $(function () {
            $('#load').on('click', function () {
                $('#grid').load('/Index?handler=CarPartial');
            });
        });
    </script>
}
@page
@模型RazorPagesAJAX.Pages.IndexModel
Razor页面AJAX示例
主页更新为-@DateTime.Now

AJAX 装载

@节脚本{ $(函数(){ $('#load')。在('click',函数(){ $(“#网格”).load(“/Index?handler=CarPartial”); }); }); }
Index.cshtml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using RazorPagesAJAX.Interfaces;                // Import interfaces
using RazorPagesAJAX.Models;                    // Needed to access List of type Car

namespace RazorPagesAJAX.Pages
{
    public class IndexModel : PageModel
    {
        // Dependency injection stuff
        private ICarService _carService;

        // Constructor Injection
        public IndexModel(ICarService carService)
        {
            _carService = carService;
        }

        [BindProperty]
        public List<Car> Cars { get; set; }

        public void OnGet()
        {
        }

        public PartialViewResult OnGetCarPartial()
        {
            Console.WriteLine("Car Partial test");
            Cars = _carService.GetAll();
            return Partial("_CarPartial", Cars);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.AspNetCore.Mvc.RazorPages;
使用Microsoft.Extensions.Logging;
使用RazorPagesAJAX.Interfaces;//导入接口
使用RazorPagesAJAX.Models;//需要访问车辆类型列表
名称空间RazorPagesAJAX.Pages
{
公共类索引模型:PageModel
{
//依赖注入的东西
私人ICarService_carService;
//构造函数注入
公共索引模型(ICarService carService)
{
_carService=carService;
}
[BindProperty]
公共列表车辆{get;set;}
公共互联网
{
}
公共部分咨询结果OnGetCarPartial()
{
控制台写入线(“汽车部分测试”);
Cars=_carService.GetAll();
返回部分(“_CarPartial”,Cars);
}
}
}
Startup.cs:

namespace RazorPagesAJAX
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            // I have added CarService to the Dependency Injection :
            services.AddTransient<ICarService, CarService>();
            services.AddRazorPages();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}
namespace RazorPagesAJAX
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
public void配置服务(IServiceCollection服务)
{
//我已将CarService添加到依赖项注入中:
services.AddTransient();
services.AddRazorPages();
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapRazorPages();
});
}
}
}
接口ICarService:

namespace RazorPagesAJAX.Interfaces
{
    public interface ICarService
    {
        // Interface for getting and returning a list of cars
        List<Car> GetAll();
    }

    // The class can use the interface by appending ": I<name>"
    public class CarService : ICarService
    {
        // The GetALL() method has some data entered for testing...
        public List<Car> GetAll()
        {
            List<Car> cars = new List<Car> {
                new Car { Id = 1, Make = "Audi", Model = "R8", Year = 2014, Doors = 2, Colour = "Red", Price = 79995 },
                new Car { Id = 2, Make = "Aston Martin", Model = "Rapide", Year = 2010, Doors = 2, Colour = "Black", Price = 54995 },
                new Car { Id = 3, Make = "Porsche", Model = " 911 991", Year = 2016, Doors = 2, Colour = "White", Price = 155000 },
                new Car { Id = 4, Make = "Mercedes-Benz", Model = "GLE 63S", Year = 2017, Doors = 5, Colour = "Blue", Price = 83995 },
                new Car { Id = 5, Make = "BMW", Model = "X6 M", Year = 2016, Doors = 5, Colour = "Silver", Price = 62995 },
            };

            return cars;
        }
    }
}
namespace RazorPagesAJAX.Interfaces
{
公共接口ICarService
{
//获取和返回车辆列表的界面
List GetAll();
}
//类可以通过附加“:I”来使用接口
公共类CarService:ICarService
{
//GetALL()方法输入了一些数据用于测试。。。
公共列表GetAll()
{
列出车辆=新列表{
新车{Id=1,Make=“Audi”,Model=“R8”,年份=2014,车门=2,颜色=“红色”,价格=79995},
新车{Id=2,Make=“Aston Martin”,Model=“Rapide”,Year=2010,Doors=2,color=“Black”,Price=54995},
新车{Id=3,Make=“Porsche”,Model=“911 991”,年份=2016,车门=2,颜色=“白色”,价格=155000},
新车{Id=4,Make=“梅赛德斯-奔驰”,Model=“GLE 63S”,年份=2017,车门=5,color=“蓝色”,价格=83995},
新车{Id=5,Make=“BMW”,Model=“X6 M”,年份=2016,车门=5,color=“Silver”,价格=62995},
};
返回车辆;
}
}
}
AJAX部分页面\u CarPartial.cshtml:

<!-- The @inject might be the problem???  --->
@inject Interfaces.CarService carsList

<table class="table table-striped">
    <!-- carsList.GetAll() is the GetAll() list in ICarService interface-->
    @foreach (var car in carsList.GetAll())
    {
        <tr>
            <td>@car.Model</td>
            <td>@car.Make</td>
            <td>@car.Year</td>
            <td>$@car.Price</td>
        </tr>
    }
</table>
<p>AJAX Updated at -  @DateTime.Now.</p>
@inject ICarService carsList

@InjectInterfaces.CarService carsList
@foreach(carsList.GetAll()中的var car)
{
@汽车模型
@汽车,制造
@汽车,一年
$@car.Price
}
AJAX更新地址为-@DateTime.Now


您需要在_CarPartial.cshtml中插入接口
ICarService
而不是继承类
CarService

<!-- The @inject might be the problem???  --->
@inject Interfaces.CarService carsList

<table class="table table-striped">
    <!-- carsList.GetAll() is the GetAll() list in ICarService interface-->
    @foreach (var car in carsList.GetAll())
    {
        <tr>
            <td>@car.Model</td>
            <td>@car.Make</td>
            <td>@car.Year</td>
            <td>$@car.Price</td>
        </tr>
    }
</table>
<p>AJAX Updated at -  @DateTime.Now.</p>
@inject ICarService carsList

谢谢你的编辑。是的,它看起来更好:)不过我使用了@injectinterfaces.ICarService carsList。