代码之家  ›  专栏  ›  技术社区  ›  Joe Ruder

href链接到不工作的视图asp。净核心2

  •  0
  • Joe Ruder  · 技术社区  · 6 年前

    我已经完成了一些关于Core 2和razor的辅导课程,但似乎无法让这件非常简单的事情顺利进行。 我拖放了一个名为Contact的本地视图。cshtml到剃刀。cshtml文件,并添加了底线,当前看起来如下所示:

        @{
        ViewData["Title"] = "About 1st Choice Web Portal";
    }
    <h2>@ViewData["Title"]</h2>
    <h3>@ViewData["Message"]</h3>
    
    <p>Internal Use Website only.</p>
    <p>Currently Under Development.</p>
    <p>For feedback please use our Contact page.</p>
    <a href="~/Views/Home/Contact.cshtml">~/Views/Home/Contact.cshtml</a>
    

    我只想在他们单击href时显示联系人视图。

    有一个联系人。视图/主页下的cshtml视图。

    HomeController。cs有:

     public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    
        public IActionResult About()
        {
            ViewData["Message"] = "--- Under Development ---";
    
            return View();
        }
    
        public IActionResult Contact()
        {
            ViewData["Message"] = "For help and support please contact one of the below departments.";
    
            return View();
        }
    

    当它运行时,会得到: 找不到此本地主机页 找不到该网址的网页: http://localhost:65061/Views/Home/Contact.cshtml

    4 回复  |  直到 6 年前
        1
  •  1
  •   Chris Pratt    6 年前

    无法链接到视图。事情不是这样的。您需要一个加载该视图的操作,然后链接到指向该操作的路由。

        2
  •  1
  •   Jacob M.    6 年前

    您正在引用。cshtml文件,我建议让路由来完成这项工作:

    <a href="~/Home/Contact">Contact</a>
    

    在MVC中。cshtml通常不直接公开。

        3
  •  0
  •   T. Sardelis    6 年前

    试着把这个

    <a asp-action="Contact" asp-controller="Home">Whatever</a>
    

    而不是你的

    <a href="/> code
    
        4
  •  -1
  •   Fernando    5 年前

    您必须在此之前包括@页,如下所示:

    @page
    @{
        ViewData["Title"] = "About 1st Choice 
    Web Portal";
    }
    

    这将使您的href被识别