MVC 3 路由帮助动态路由

发布于 2024-12-06 21:37:10 字数 2045 浏览 2 评论 0原文

我试图做这样的事情:

routes.MapRoute("Product", "{product}/{id}",
                            new
                                {
                                    action = "Product",
                                    controller = "Home",
                                    product = UrlParameter.Optional,
                                    id = UrlParameter.Optional
                                });

当我尝试加载页面 404 时,它给了我错误,我想, 我试图使网址看起来像这样: www.tables.com/productName/ID 。 我怎样才能做到这一点而不添加像这样的强类型词:

routes.MapRoute("Product", "Products/{product}/{id}", ... )

其余路线:

 routes.MapRoute("Product", "{product}/{id}",
                            new
                                {
                                    action = "Product",
                                    controller = "Home",
                                    product = UrlParameter.Optional,
                                    id = UrlParameter.Optional
                                });

            routes.MapRoute("Category", "Category/{category}/{template}",
                            new
                            {
                                action = "Index",
                                controller = "Category",
                                category = UrlParameter.Optional,
                                template = UrlParameter.Optional
                            });

            routes.MapRoute("Profile", "Profile/{fullName}",
                           new
                           {
                               action = "Index",
                               controller = "Profile",
                               fullName = UrlParameter.Optional
                           });


            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );

谢谢。

Im trying to do something like this:

routes.MapRoute("Product", "{product}/{id}",
                            new
                                {
                                    action = "Product",
                                    controller = "Home",
                                    product = UrlParameter.Optional,
                                    id = UrlParameter.Optional
                                });

It gives me error when im trying to load page 404 i think,
Im trying to make the url look like this: www.tables.com/productName/ID .
How can i do it without adding a strong type word like this:

routes.MapRoute("Product", "Products/{product}/{id}", ... )

rest of the routes:

 routes.MapRoute("Product", "{product}/{id}",
                            new
                                {
                                    action = "Product",
                                    controller = "Home",
                                    product = UrlParameter.Optional,
                                    id = UrlParameter.Optional
                                });

            routes.MapRoute("Category", "Category/{category}/{template}",
                            new
                            {
                                action = "Index",
                                controller = "Category",
                                category = UrlParameter.Optional,
                                template = UrlParameter.Optional
                            });

            routes.MapRoute("Profile", "Profile/{fullName}",
                           new
                           {
                               action = "Index",
                               controller = "Profile",
                               fullName = UrlParameter.Optional
                           });


            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺孤伤于心 2024-12-13 21:37:10

您的问题是产品路线将匹配不以类别或配置文件开头的所有内容。

我会将产品路由放在默认路由之前,并使用 IRouteConstraint,使其不匹配非产品。

代码示例:

routes.MapRoute("Category", "Category/{category}/{template}",
                new
                {
                    action = "Index",
                    controller = "Category",
                    category = UrlParameter.Optional,
                    template = UrlParameter.Optional
                });

routes.MapRoute("Profile", "Profile/{fullName}",
               new
               {
                   action = "Index",
                   controller = "Profile",
                   fullName = UrlParameter.Optional
               });


routes.MapRoute("Product", "{product}/{id}",
                new
                    {
                        action = "Product",
                        controller = "Home",
                        product = UrlParameter.Optional,
                        id = UrlParameter.Optional
                    },
                new { product = new ProductRouteConstraint() });

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
);

以及路由约束:

public class ProductRouteConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (routeDirection == RouteDirection.IncomingRequest &&
                parameterName.ToLowerInvariant() == "product")
            {
                var productName = values[parameterName] as string;
                if (productName == null) 
                    return false;

                var productId = values["id"] as string;
                if (productId == null)
                    returns false;

                return ProductCatalogue.HasProductById(productId);
            }

            return false;
        }
    }

ProductCatalogue 显然应该替换为您在系统中查找产品的方式。

Your problem is that the Product route will match everything not starting with Category or Profile.

I would place the product route just before the default route and use a IRouteConstraint such that it doesn't match non products.

Code sample:

routes.MapRoute("Category", "Category/{category}/{template}",
                new
                {
                    action = "Index",
                    controller = "Category",
                    category = UrlParameter.Optional,
                    template = UrlParameter.Optional
                });

routes.MapRoute("Profile", "Profile/{fullName}",
               new
               {
                   action = "Index",
                   controller = "Profile",
                   fullName = UrlParameter.Optional
               });


routes.MapRoute("Product", "{product}/{id}",
                new
                    {
                        action = "Product",
                        controller = "Home",
                        product = UrlParameter.Optional,
                        id = UrlParameter.Optional
                    },
                new { product = new ProductRouteConstraint() });

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
);

And the route constraint:

public class ProductRouteConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (routeDirection == RouteDirection.IncomingRequest &&
                parameterName.ToLowerInvariant() == "product")
            {
                var productName = values[parameterName] as string;
                if (productName == null) 
                    return false;

                var productId = values["id"] as string;
                if (productId == null)
                    returns false;

                return ProductCatalogue.HasProductById(productId);
            }

            return false;
        }
    }

The ProductCatalogue should obviously be replaced with however you lookup products in your system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文