1 - 我从传递两个参数的 WebApi Controller 生成一个链接:
var URL = new Uri( Url.Link("Default", new { Controller = "Reset", Action =
"ResetPassword",user = "AAAAA", hash = "HASHVALUE" }));
我明白了:
http://localhost:52494/Reset/ResetPassword?user=BBBBBBBB&hash=AAAAAAA
2 - 我还需要阅读这两个参数和表格。
所以我有一个带有ResetPassword
操作的 Controller Reset
:
public ActionResult ResetPassword()
{
return View();
}
[HttpPost]
public ActionResult ResetPassword(Models.ResetUserModel user)
{
var HASH = Request["hash"];
var id = Request["user"];
return View();
}
和 cshtml 文件:
如果我运行此页面并填写表单,我将能够读取 ResetUserModel
,但如果它有一些参数,模型将变为空!!
我在这里做错了什么?
最佳答案
您似乎没有在操作中定义相关参数,因为您从 URI 传递了两个参数:
user = "AAAAA", hash = "HASHVALUE"
var URL = new Uri( Url.Link("Default", new { Controller = "Reset", Action =
"ResetPassword",user = "AAAAA", hash = "HASHVALUE" }));
所以你必须根据这样的定义你的 Action :
[HttpGet]
public ActionResult ResetPassword(string user, string hash)
{
var user = user;
var hash = hash;
return View();
}
关于c# - 如何从 url Aspnet WebApi 读取字符串参数和表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45056879/