'Cannot show razor view in browser (ASP.NET Core MVC)
I am really new to this ASP.NET Core MVC stuff I have this problem: browser is showing "Page not found 404" error.
Here is my code:
Model class (inside Models folder):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Http;
using System.ComponentModel;
namespace CasaSelectaAppWeb.Models
{
public class HabitatClass
{
[Key]
[StringLength(100)]
public string CodeHab { get; set; }
[Required]
[StringLength(30)]
public string DesignationHab { get; set; }
[Required]
public int Superficie { get; set; }
[Required]
[Range (1,40)]
public int NbrPieces { get; set; }
[Required]
[DisplayName("Prix Initial")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "Veuillez entrer une valeur correcte !")]
public int prixInit { get; set; }
[Required]
[DisplayName("Prix négotcié")]
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "Veuillez entrer une valeur correcte !")]
public int prixNego { get; set; }
[Required]
[StringLength(20)]
public string LocalHab { get; set; }
[Required]
[StringLength(200)]
public string AdressHab { get; set; }
[Required]
[StringLength(400)]
public string DescriptionHab { get; set; }
[MaxLength]
//public byte[] ImageUrl { get; set; }
//[Required(ErrorMessage ="Veuillez ajouter une image !")]
//[Display(Name ="Image")]
//[NotMapped]
//public IFormFile ImageHab { get; set; }
//CleEtrngVente
[Display(Name ="Type d'habitat")]
[StringLength(30)]
public virtual string TypeHpk { get; set; }
[ForeignKey("TypeHpk")]
public virtual TypeHabitat TypesHab { get; set; }
}
}
Html markup:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - CasaSelectaAppWeb</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">CasaSelectaAppWeb</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="CasaSelecta" asp-action="Index">Liste des habitats</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2022 - CasaSelectaAppWeb - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>
Controller
using CasaSelectaAppWeb.Data;
using CasaSelectaAppWeb.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CasaSelectaAppWeb.Controllers
{
public class HabitatsController : Controller
{
private readonly DbContClass _context;
public HabitatsController(DbContClass context)
{
_context = context;
}
public IActionResult Index()
{
List<HabitatClass> habitats;
habitats = _context.Habitats.ToList();
return View(habitats);
}
}
}
Solution 1:[1]
1.check you have configured routing in startup.cs:
app.UseRouting();
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
2.Check if your routing exists.For example,routing of Index in your code is
https://localhost:xxx/Habitats/Index
3.Check if your corresponding view is in the correct location.For example the view of Index should in Views/Habitats,and the name should be Index.cshtml.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yiyi You |
