如何解决无法将 [] 索引应用于 HttpRequest 类型的表达式
我对编程很陌生,我正在尝试学习 asp.net 网络开发。我正在尝试创建一个表单来接收用户名和密码,然后将其发送到随附的 post action 方法,但是当我尝试从 action 方法请求数据时,它会出现一个错误,提示“无法应用索引[] 到类型为 HttpRequest' 的表达式。我真的不知道我在做什么,如果有人能告诉我我做错了什么,我将不胜感激。谢谢!
这是 HTML
@{
ViewData["Title"] = "Login";
}
<div class="text-center">
<form method="post" action="Index">
<label for="username">Username: </label>
<input type="text"id="username" />
<label for=" password">Password: </label>
<input type ="text"id="password" />
<input type="submit" value="Submit"/>
</form>
</div>
这是动作方法
public IActionResult Index()
{
return View();
}
[HttpPost]
[ActionName("Index")]
public IActionResult IndexPost()
{
string username = Request["username"]; #This is where the error is happening
string password = Request["password"]; #Red line under both requests
-stuff to do-
return Content("");
}
解决方法
ASP.Net 具有模型绑定,可用于自动将 Action 参数绑定到发布的值,因此您应该能够做到这一点
[HttpPost]
[ActionName("Index")]
public IActionResult IndexPost(string username,string password)
{
-stuff to do-
return Content("");
}
错误的原因是 Request
不像字典那样可索引。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。