我给点的重要的代码吧
public ActionResult Blog(int id)
{
Regex reg = new Regex("^[0-9]+$");
Match ma = reg.Match(id.ToString());
if (!ma.Success)
{
return RedirectToAction("Error");
}
else
{
var article = db.Articles.Find(id);
if (article == null)
{
return RedirectToAction("Error");
}
else
{
articlerepository.UpdateCount(id);
ViewBag.sitename = db.BlogConfigs.First().BlogName;
这里我主要是防止用户输入非纯数字。
当id不是一个数字时,就跳转到action Error上。
可是我在测试时,当id是一个数字时,则正常。而当id不是一个数字时,没有跳转到Error,而是直接报错了。
报错代码如下:
“/”应用程序中的服务器错误。
对于“Blog.Controllers.ReadController”中方法“System.Web.Mvc.ActionResult Blog(Int32)”的不可以为 null 的类型“System.Int32”的参数“id”,参数字典包含一个 null 项。可选参数必须为引用类型、可以为 null 的类型或声明为可选参数。
参数名: parameters
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息: System.ArgumentException: 对于“Blog.Controllers.ReadController”中方法“System.Web.Mvc.ActionResult Blog(Int32)”的不可以为 null 的类型“System.Int32”的参数“id”,参数字典包含一个 null 项。可选参数必须为引用类型、可以为 null 的类型或声明为可选参数。
参数名: parameters
我应该怎么处理这个问题?
public ActionResult Blog(int id)
{
Regex reg = new Regex("^[0-9]+$");
Match ma = reg.Match(id.ToString());
if (!ma.Success)
{
return RedirectToAction("Error");
}
else
{
var article = db.Articles.Find(id);
if (article == null)
{
return RedirectToAction("Error");
}
else
{
articlerepository.UpdateCount(id);
ViewBag.sitename = db.BlogConfigs.First().BlogName;
这里我主要是防止用户输入非纯数字。
当id不是一个数字时,就跳转到action Error上。
可是我在测试时,当id是一个数字时,则正常。而当id不是一个数字时,没有跳转到Error,而是直接报错了。
报错代码如下:
“/”应用程序中的服务器错误。
对于“Blog.Controllers.ReadController”中方法“System.Web.Mvc.ActionResult Blog(Int32)”的不可以为 null 的类型“System.Int32”的参数“id”,参数字典包含一个 null 项。可选参数必须为引用类型、可以为 null 的类型或声明为可选参数。
参数名: parameters
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息: System.ArgumentException: 对于“Blog.Controllers.ReadController”中方法“System.Web.Mvc.ActionResult Blog(Int32)”的不可以为 null 的类型“System.Int32”的参数“id”,参数字典包含一个 null 项。可选参数必须为引用类型、可以为 null 的类型或声明为可选参数。
参数名: parameters
我应该怎么处理这个问题?
解决方案 »
- repeater绑定字段时怎么运算别的字段的值
- 紧急求助:asp.net调用java写的webservice,已附件的形式发送文件到webservice
- 在用户控件中设置的背景图为什用到界面里就显示不出来了?
- 关于asp.net中会话问题的超级提问!!!!!!!!
- 请问谁用过ActiveReport for .net2.0
- [讨论]我想知道招聘中的精通到底是什么程度!
- repeater里面的值如何递增?
- 如何在 .css 文件中写table 的不同式样?
- 100分求一个关于定时激发的解决方案——在线等
- @Html.TextBoxFor
- "ORA-01036: 非法的变量名/编号\n"
- proMsg不包含getProMsg的定义,并且找不到可接受类型为proMsg的第一个参数的扩展方法getProMsg
Regex reg = new Regex("^[0-9]+$");
string result = id.ToString();
if (!reg.IsMatch(result))
{
return RedirectToAction("Error");
}
public ActionResult Blog(int? id)
=>
public ActionResult Blog(int? id)Regex reg = new Regex("^[0-9]+$");
Match ma = reg.Match(id.ToString());
=>
if(!id.HasValue)
RedirectToAction("Error");
Regex reg = new Regex("^[0-9]+$");
Match ma = reg.Match(id.Value.ToString());