MVC RouteConfig定制图路线问题 [英] MVC RouteConfig custom maproute issue
问题描述
我有这在我的routeConfig:
I have this in my 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 code:
and this is MyProductConstraint code :
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;
}
}
它的工作原理,但如果我补充一点:
it works, but if I add this :
INT ID =(int)的值[thisID];
和改变这一行:
返回db.tbl_Product.Any(C => c.url == URL);
return db.tbl_Product.Any(c => c.url==url);
要:
返回db.tbl_Product.Any(C => c.url == URL和放大器;&安培; c.ID == ID);
我给这个错误在应用程序运行时: 指定的转换是无效的
I give this error when application runs : Specified cast is not valid.
在这里什么事?!
推荐答案
我已经改变了
INT ID =(int)的值[thisID];
int id = (int)values["thisID"];
到
INT ID = Convert.ToInt32(值[thisID]的ToString());
和还routes.MapRouteLowercase以图路线和问题解决了,我一点问题去LowercaseRoutesMVC DLL,我必须将此情况报告给他们的开发。
and also routes.MapRouteLowercase to MapRoute and problem solved, I thing the issue goes to LowercaseRoutesMVC DLL and I have to report this to their developer.
这篇关于MVC RouteConfig定制图路线问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!