public class Startup
{
private IConfigurationRoot _confSting;
public Startup(IWebHostEnvironment hostEnv)
{
_confSting = new ConfigurationBuilder().SetBasePath(hostEnv.ContentRootPath).AddJsonFile("dbsettings.json").Build();
}
// 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, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDBContent>(options => options.UseSqlServer(_confSting.GetConnectionString("DefaultConnection")));
services.AddControllersWithViews();
services.AddTransient<IAllCars, CarRepository>();
services.AddTransient<ICarsCategory, CategoryRepository>();
services.AddTransient<IAllOrders, OrdersRepository>();
services.AddTransient<ShopCart>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped(sp => ShopCart.GetCart(sp));
services.AddSession();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "categoryFilter",
pattern: "Car/{action}/{category?}", defaults: new { Controller="Car", action="List"});
});
app.UseStaticFiles();
using (var scope = app.ApplicationServices.CreateScope())
{
AppDBContent content = scope.ServiceProvider.GetRequiredService<AppDBContent>();
DBObjects.Initial(content);
}
}
}
Вы добавили контролеры в контейнер, но не заиспользовали их.вы имеете виду интерфейсы и классы которые реализуют их? Если это так, то я их реализовал
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication29.Data.interfaces;
using WebApplication29.Data.mocks;
namespace WebApplication29
{
public class Startup
{
// 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, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddTransient<IAllCars, MockCars>();
services.AddTransient<ICarsCategory, MockCategory>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
app.UseStaticFiles();
}
}
}
P.S Лучше для фильтрации на контроллер отправлять не имя, а Id.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Include="Views\Home\Index.cshtml" />
<None Include="Views\Home\Privacy.cshtml" />
<None Include="Views\Shared\Error.cshtml" />
<None Include="Views\Shared\_Layout.cshtml" />
<None Include="Views\Shared\_ValidationScriptsPartial.cshtml" />
<None Include="Views\_ViewImports.cshtml" />
<None Include="Views\_ViewStart.cshtml" />
<None Include="wwwroot\js\site.js" />
<None Include="wwwroot\lib\bootstrap\dist\js\bootstrap.bundle.min.js" />
<None Include="wwwroot\lib\bootstrap\dist\js\bootstrap.min.js" />
<None Include="wwwroot\lib\bootstrap\LICENSE" />
<None Include="wwwroot\lib\jquery-validation-unobtrusive\jquery.validate.unobtrusive.js" />
<None Include="wwwroot\lib\jquery-validation-unobtrusive\jquery.validate.unobtrusive.min.js" />
<None Include="wwwroot\lib\jquery-validation\dist\additional-methods.js" />
<None Include="wwwroot\lib\jquery-validation\dist\additional-methods.min.js" />
<None Include="wwwroot\lib\jquery-validation\dist\jquery.validate.js" />
<None Include="wwwroot\lib\jquery-validation\dist\jquery.validate.min.js" />
<None Include="wwwroot\lib\jquery-validation\LICENSE.md" />
<None Include="wwwroot\lib\jquery\dist\jquery.js" />
<None Include="wwwroot\lib\jquery\dist\jquery.min.js" />
<None Include="wwwroot\lib\jquery\dist\jquery.min.map" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-preview.2.21154.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-preview.1.21102.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>