ASP.NET 路由配置不遵循教程

ASP.NET Routes config not following tutorial

我正在尝试按照基本教程使表单正常工作: https://www.completecsharptutorial.com/asp-net-mvc5/4-ways-to-create-form-in-asp-net-mvc.php

在第一个示例 运行 之后,我的代码尝试呈现 localhost:5001/form1 而不是示例的 localhost:5001/Home/form1

我假设问题是我的 Starup.cs 路线需要调整。目前看起来像:

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

控制器代码:

namespace test_ops.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = 
ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = 
Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        [HttpPost] 
        public ActionResult form1(int txtId, string txtName, string chkAddon)
        {
            ViewBag.Id = txtId;
            ViewBag.Name = txtName;
            if (chkAddon != null)
                ViewBag.Addon = "Selected";
            else
                ViewBag.Addon = "Not Selected";

            return View("Index");
        }
    }
}

型号代码:

namespace test_ops.Models
{
    public class TestModel
    {
        [Required]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Addon { get; set; }
    }
}

查看 Home/index.cshtml 中的代码:

<h4 style="color:purple">
<b>ID:</b>    @ViewBag.ID <br />
<b>Name:</b>  @ViewBag.Name <br />
<b>Addon:</b> @ViewBag.Addon
</h4>
<hr />
<h3><b>Forms: Weakly Typed</b></h3>

<form action="form1" method="post">
    <table>
        <tr>
            <td>Enter ID: </td>
            <td><input type="text" name="txtId" /></td>
        </tr>
        <tr>
            <td>Enter Name: </td>
            <td><input type="text" name="txtName" /></td>
        </tr>
        <tr>
            <td>Addon: </td>
            <td><input type="checkbox" name="chkAddon" /></td>
        </tr>
        <tr>
                <td colspan="2"><input type="submit" value="Submit Form" /></td>
        </tr>
    </table>
</form>

提交表单后,页面返回 404: "This localhost page can't be found没有找到网址的网页:https://localhost:5001/form1 HTTP 错误 404

有人可以告诉我需要更改什么才能使它正常工作吗?

尝试将控制器添加到您的表单

<form action="@Url.Action("form1", "home")" method="post">
...

但是使用这种语法比使用形式更好

@using (Html.BeginForm("form1", "Home", FormMethod.Post))
{   
 <table>
        <tr>
    //...    
    
  <tr>
   <td colspan="2"><input type="submit" value="Submit Form" /></td>
        </tr>
}

并检查 View 文件夹中的文件 _ViewImports.cshtml。应该有

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers