Answer the question
In order to leave comments, you need to log in
Web page not showing up?
I registered the controller path + method name (view name) but returns a 404 error. What should I do?
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddTransient<IAllCars, MockCars>();
services.AddTransient<ICarsCategory, MockCategory>();
}
@using WebApplication29.Data.Models
@model IEnumerable<Car>
@{
Layout = null;
}
<!DOCKTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>List</title>
</head>
<body>
<h2>Все автомобили</h2>
@{
foreach(var car in Model)
{
<div>
<h2>Модель: @car.name</h2>
<p>Цена: @car.price.ToString("c")</p>
</div>
}
}
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication29.Data.Models
{
public class Car
{
public int id { set; get; }
public string name { set; get; }
public string shortDesc { set; get; }
public string longDesc { set; get; }
public string img { set; get; }
public ushort price { set; get; }
public bool isFavourite { set; get; }
public bool available { set; get; }
public int categoryId { set; get; }
public virtual Category Category { set; get; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication29.Data.interfaces;
using WebApplication29.Data.Models;
namespace WebApplication29.Data.mocks
{
public class MockCars : IAllCars
{
private readonly ICarsCategory _categoryCars = new MockCategory();
public IEnumerable<Car> Cars
{
get
{
return new List<Car>
{
new Car {
name="Tesla",
shortDesc="Автомобиль класса люкс",
longDesc="Стильный дизайн",
img="img/Tesla.jpg",
price=45000,
isFavourite=true,
available=true,
Category = _categoryCars.AllCategories.First()
},
new Car {
name="Porsche",
shortDesc="Дерзкий автомобиль",
longDesc="Красивый и скоростной",
img="img/Porsche.jpg",
price=45000,
isFavourite=true,
available=true,
Category = _categoryCars.AllCategories.Last()
},
new Car {
name="Nissan",
shortDesc="Удобный",
longDesc="Автомобиль для городской жизни",
img="img/Nissan.jpg",
price=45000,
isFavourite=true,
available=true,
Category = _categoryCars.AllCategories.Last()
},
new Car {
name="Cadillac",
shortDesc="Стильный",
longDesc="Автомобиль идеално впишется в интерьер",
img="img/Cadilac/jpg",
price=45000,
isFavourite=true,
available=true,
Category = _categoryCars.AllCategories.First()
},
};
}
}
public IEnumerable<Car> getFavCars { get; set; }
IEnumerable<Car> IAllCars.Cars { get => throw new NotImplementedException(); }
public Car getObjectCar(int carId)
{
throw new NotImplementedException();
}
}
}
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApplication29.Data.interfaces;
namespace WebApplication29.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 Index()
{
var cars = _allCars.Cars;
return View();
}
}
}
Answer the question
In order to leave comments, you need to log in
You misspelled startup.
https://metanit.com/sharp/aspnet5/3.6.php
Namely, they forgot to map the controllers in UseEndpoints
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question