我已将此添加到我的MVC RouteConfig:
routes.MapRoute(
name: "categories",
url: "{sub}",
defaults: new { controller = "cat", action = "Index" }
, constraints: new { sub = new MyCatConstraint() }
);
这是我的MyCatConstraint类:
public class MyCatConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
KhanoumiDbContext db = new KhanoumiDbContext();
if (values.ContainsKey(parameterName))
{
string thisCat = values["sub"].ToString();
return db.tbl_Category.Any(c => c.Cat_Name_En == thisCat);
}
return false;
}
}
添加这些后,我有时会收到此错误:
ExecuteReader需要一个开放且可用的连接。连接的当前状态已打开。
你能告诉我怎么办才能解决这个问题吗?答案 0 :(得分:2)
在KhanoumiDbContext db = new KhanoumiDbContext()
声明中包裹using
。