有人知道如何解决这个错误吗?
指定的转换无效。
描述:在执行当前网络请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.InvalidCastException:指定的转换无效。
这是中断的代码行 -
var id = (int)httpContext.Request.RequestContext.RouteData.Values["id"];
最佳答案
httpContext.Request.RequestContext.RouteData.Values["id"]
可能是一个字符串,因此您需要解析它:
var idStr=(string)(httpContext.Request.RequestContext.RouteData.Values["id"]);
int id;
if(int.TryParse(idStr, out id))
{
//w00t
}
关于c# - MVC3/C# - 无效的转换异常 - 指定的转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11251688/