【发布时间】:2016-03-23 07:05:11
【问题描述】:
我的 routeConfig 中有这个:
routes.MapRouteLowercase(
name: "newProduct",
url: "{name}-{thisID}",
defaults: new
{
controller = "newProduct",
action = "Index",
name = UrlParameter.Optional
},
constraints: new { name = new MyProductConstraint() },
namespaces: new string[] { "khanoumiMetro.Controllers" }
);
这是 MyProductConstraint 代码:
public class MyProductConstraint : IRouteConstraint
{
private KhanoumiDbContext db = new KhanoumiDbContext();
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (values.ContainsKey(parameterName))
{
string url = values["name"].ToString();
using (KhanoumiDbContext db = new KhanoumiDbContext())
{
db.Database.Connection.Open();
return db.tbl_Product.Any(c => c.url==url);
}
}
return false;
}
}
它有效,但如果我添加这个:
int id = (int)values["thisID"];
并更改此行:
返回 db.tbl_Product.Any(c => c.url==url);
收件人:
return db.tbl_Product.Any(c => c.url==url && c.ID==id);
应用程序运行时出现此错误:Specified cast is not valid.
这里发生了什么?!
【问题讨论】:
-
id变量的类型是什么?
-
c.ID 和 id 类型相同?
-
@Thomas 这是来自我的模型:public int ID { get;放; }
-
您能否发布您的异常的详细信息并指定引发异常的行
-
@Thomas 我认为问题出在我使用的 LowercaseRoutesMVC DLL 上,因为将 routes.MapRouteLowercase 替换为 MapRoute 解决了问题!
标签: c# asp.net-mvc asp.net-routing maproute