Не отображается содержимое HTML
Код:
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Shop.Data.interfaces;
using Shop.Data.mocks;
namespace Shop
{
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.AddTransient<IAllCars, MockCars>();
services.AddTransient<ICarsCategory, MockCategory>();
services.AddMvc(); //
services.AddRazorPages();
services.AddMvc(options => options.EnableEndpointRouting = false);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMvcWithDefaultRoute();
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
app.UseStaticFiles();
}
}
}
СarsController.cs
using Microsoft.AspNetCore.Mvc;
using Shop.Data.interfaces;
using Shop.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Shop.Controllers
{
public class CarsController : Controller { // унаследовали от Класса Контролер
private readonly IAllCars _allCars; // переменная на интерфей ( интерфейс+ класс с реализацией)
private readonly ICarsCategory _allCategories;
public CarsController(IAllCars iAllCars , ICarsCategory iCarsCat)
{
_allCars = iAllCars;
_allCategories = iCarsCat;
}
public ViewResult List() { // обращаемся к ф-ции List и получаем html страничку
ViewBag.Title = "Страница с автомобилями";
CarsListViewModel obj = new CarsListViewModel();
obj.allCars = _allCars.Cars;
obj.currCategory = "Автомобили";
return View(obj); // получение htlm
List.cshtml
<h1>Все автомобили</h1>
<h3>@Model.currCategory</h3>
@{
foreach (var car in Model.allCars)
{
<div>
<h2>Модель: @car.name</h2>
<p>Цена:@car.price.ToString("c")</p>
</div>
}
}
Ответы (1 шт):
Автор решения: Scrim
→ Ссылка
Ты отключил маршрутизацию по конечным точкам, ну так самой маршрутизации у тебя нет. Либо добавляй обработчик маршрута в методе configure, через метод расширения UseMvc, либо в самом контроллере через атрибуты пропиши путь, по типу
[Route("List")]
public IActionResult List()...
