asp.net - 路由 ASP.Net MVC 的问题
问题描述
当我点击一个类后使用 ISS 运行应用程序时。浏览器使用此 url 启动。http://localhost:50282/
当我在我的文件夹 Account In Views 中单击 Index 并运行应用程序时,我得到这个 url:http://localhost:50282/Account/Index
现在,在这两个 url 上,我都有一个注册表单,它链接到我的 AccountController 中的一个操作。当我在第二种情况下提交表单时,我得到这个 url: http://localhost:50282/Account/register并且 register 方法在我的 AccountController 类中运行并且工作正常。
在第一种情况下,我得到这个 url 和错误:
网址:http://localhost:50282/register
错误:“/”应用程序中的 404
服务器错误。
无法找到该资源。
说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。请求的 URL:/注册
我希望 url 在单击注册后转到第二个 url http://localhost:50282/Account/register ,无论我在运行应用程序之前单击了哪里。
提交表单查看代码:
@{
ViewBag.Title = "Index";
}
<h1>Register Form </h1>
<form action="register" method="post">
<label><i class="" aria-hidden="true"></i> Username </label>
<input type="text" name="Username" placeholder="Enter User Name" required="" />
<br>
<label><i class="" aria-hidden="true"></i> password </label>
<input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" />
<input type="submit" value="Register">
</form>
解决方案
您需要指定表单的提交位置。所以改变这个...
<form action="register" method="post">
<label>
<i class="" aria-hidden="true"></i> Username </label>
<input type="text" name="Username" placeholder="Enter User Name" required="" />
<br>
<label>
<i class="" aria-hidden="true"></i> password </label>
<input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" /> <input type="submit" value="Register">
</form>
到您的表单的 html 助手...
@using(Html.BeginForm("Register", "Account"))
{
<label><i class="" aria-hidden="true"></i> Username </label>
<input type="text" name="Username" placeholder="Enter User Name" required="" />
<br>
<label><i class="" aria-hidden="true"></i> password </label>
<input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" />
<input type="submit" value="Register">
}
您还需要添加antiforgery token
安全性 ( @html.Antiforgerytoken()
)。并用属性装饰Register
动作。[ValidateAntiforgeryToken]
看到这个
推荐阅读
- sql-server - 在经典asp中将数据库数据导出到csv
- javascript - 在Javascript中调用类的其他静态方法中的静态方法
- spring-boot - 在子路径上运行后,Spring Boot App 不再正常工作
- sparql - wikidata SPARQL 获取属性:使用 SAMPLE() 时的标签
- apache-kafka - Kafka 中的 CreateTopicPolicy 未在创建主题时调用 validate()
- c# - 如何使用 postgresql 和 .NET Core 创建引用跨多个表的主键的外键?
- regex - 如何为需要多次验证的字符串构建正则表达式
- html - Onclick 功能在动作链接标签中的 href 中自动添加 - Asp.Net Core
- html - HTML 表格在表格列中正确使用和设置 mat-select
- java - 如何处理瞬态类加载错误,例如线程中断