MVC RouteConfig定制图路线问题 [英] MVC RouteConfig custom maproute issue

查看:192
本文介绍了MVC RouteConfig定制图路线问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这在我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆