【问题标题】:MVC RouteConfig custom maproute issueMVC RouteConfig 自定义地图路由问题
【发布时间】: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


【解决方案1】:

我变了

int id = (int)values["thisID"];

int id = Convert.ToInt32(values["thisID"].ToString());

还有 routes.MapRouteLowercase 到 MapRoute 并解决了问题,我认为问题出在 LowercaseRoutesMVC DLL 上,我必须向他们的开发人员报告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多