MVC RouteConfig 自定义 maproute 问题
MVC RouteConfig custom maproute issue
我的路由配置中有这个:
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"];
并更改此行:
return 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.
这里发生了什么?!
我变了
int id = (int)values["thisID"];
到
int id = Convert.ToInt32(值["thisID"].ToString());
还有 routes.MapRouteLowercase 到 MapRoute 并解决了问题,我认为问题出在 LowercaseRoutesMVC DLL 上,我必须将此报告给他们的开发人员。
我的路由配置中有这个:
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"];
并更改此行:
return 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.
这里发生了什么?!
我变了
int id = (int)values["thisID"];
到
int id = Convert.ToInt32(值["thisID"].ToString());
还有 routes.MapRouteLowercase 到 MapRoute 并解决了问题,我认为问题出在 LowercaseRoutesMVC DLL 上,我必须将此报告给他们的开发人员。