C# HttpPost和HttpGet
我的HttpPost需要一些帮助。我不知道在HttpPost上写什么才能在我看来使用我的工作代码 我已经编写了生成密码的代码:C# HttpPost和HttpGet,c#,http-post,http-get,C#,Http Post,Http Get,我的HttpPost需要一些帮助。我不知道在HttpPost上写什么才能在我看来使用我的工作代码 我已经编写了生成密码的代码: private static string PasswordGenerator(int passwordLength, bool strongPassword) { int seed = Random.Next(1, int.MaxValue); //const string allowedChars = "ABCDEFGHJKLMNOPQRST
private static string PasswordGenerator(int passwordLength, bool strongPassword)
{
int seed = Random.Next(1, int.MaxValue);
//const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
const string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
const string specialCharacters = @"!#$%&'()*+,-./:;<=>?@[\]_";
var chars = new char[passwordLength];
var rd = new Random(seed);
for (var i = 0 ; i < passwordLength; i++)
{
// If we are to use special characters
if (strongPassword && i % Random.Next(3, passwordLength) == 0 )
{
chars[i] = specialCharacters[rd.Next(0 , specialCharacters.Length)];
}
else
{
chars[i] = allowedChars[rd.Next(0 , allowedChars.Length)];
}
}
return new string(chars);
}
所有html输入都应该具有分配给这些内容的“Name”属性。。 e、 g
基本上,您应该在mvc中使用强类型视图,这样您就可以在POST操作中获得填充的模型,但由于您没有添加任何类型特定的视图,因此您可以通过表单集合访问发布的值。在哪里有??您需要输入:
.PasswordGenerator(…)
这个“…”应该是PasswordGenerator()
方法所需的参数,
应该是静态方法所在的类的名称
例如,您的代码可能看起来有点像这样,在您的视图中有更多的连线:
[HttpGet]
public ActionResult Generate()
{
return View("Index");
}
[HttpPost]
public ActionResult PasswordGenerator(PasswordModel model)
{
<nameofclass>.PasswordGenerator(model.Password.Length, model.StrongPassword);
}
[HttpGet]
公共行动结果生成()
{
返回视图(“索引”);
}
[HttpPost]
公共操作结果密码生成器(密码模型)
{
.PasswordGenerator(model.Password.Length,model.strong密码);
}
正如KD所建议的,我将向所有表单元素添加名称属性
<input type="text" name="length" value="8" id="length_field">
<input type="checkbox" id="includeLetters" checked>
如果参数的数量超过了您认为合适的数量,只需使用与表单字段匹配的[properties]创建一个对象即可。即:
public class PasswordGeneratorArguments {
public int Length {get;set}
public bool IncludeLetters {get;set}
etc...
}
并以此作为论据
[HttpPost]
public ActionResult PasswordGenerator(PasswordGeneratorArguments model)
{
}
[HttpGet]
public ActionResult Generate()
{
return View("Index");
}
[HttpPost]
public ActionResult PasswordGenerator(PasswordModel model)
{
<nameofclass>.PasswordGenerator(model.Password.Length, model.StrongPassword);
}
<input type="text" name="length" value="8" id="length_field">
<input type="checkbox" id="includeLetters" checked>
[HttpPost]
public ActionResult PasswordGenerator(int length, bool includeLetters, etc...)
{
}
public class PasswordGeneratorArguments {
public int Length {get;set}
public bool IncludeLetters {get;set}
etc...
}
[HttpPost]
public ActionResult PasswordGenerator(PasswordGeneratorArguments model)
{
}