【问题标题】:Cannot show razor view in browser (ASP.NET Core MVC)无法在浏览器中显示剃刀视图(ASP.NET Core MVC)
【发布时间】:2022-02-10 11:09:49
【问题描述】:

我对这个 ASP.NET Core MVC 东西真的很陌生,我遇到了这个问题:浏览器显示“找不到页面 404”错误。

这是我的代码:

模型类(在Models 文件夹内):

    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 标记:

<!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">
            &copy; 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>

控制器

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);
        }
    }
}

【问题讨论】:

    标签: razor asp.net-core-mvc


    【解决方案1】:

    1.检查你是否在startup.cs中配置了路由:

    app.UseRouting();
    
    
                ...
                app.UseEndpoints(endpoints =>
                {
    
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                    
                   
                });
    

    2.检查你的路由是否存在。例如你的代码中Index的路由是

    https://localhost:xxx/Habitats/Index
    

    3.检查你对应的视图是否在正确的位置。例如Index的视图应该在Views/Habitats,名称应该是Index.cshtml。

    【讨论】:

    • 我想我已经完成了 Microsoft 和他们“酷”的东西,我又开始用颤振了……;比 Microsoft bs 好多了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2018-03-09
    相关资源
    最近更新 更多