简单的 MVC 路线遇到问题

发布于 2024-10-10 01:38:00 字数 521 浏览 4 评论 0原文

某些路线遇到一些问题。我并不完全理解 MVC 路由系统,所以请耐心等待。

我有两个控制器,产品和主页(还有更多控制器!)。

我希望无需在 url 中键入 Home 即可访问 Home 控制器中的视图。本质上,我想将 www.example.com/home/about 转换为 www.example.com/about,但我仍然想保留 www.example.com/products。

这是我到目前为止所拥有的。

routes.MapRoute( "Home", "{action}", new { controller = "Home" } );

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "home", action = "index", id = UrlParameter.Optional }
);

现在,根据哪个是第一个,我可以让其中一个工作,但不能同时工作。

Having some trouble with some routes. I don't fully understand the MVC routing system so bear with me.

I've got two controllers, Products and Home (with more to come!).

I want to have the views within the Home controller accessible without having to type Home in the url. Essentially I want to turn www.example.com/home/about into www.example.com/about, however I still want to preserve the www.example.com/products.

Here's what I have so far.

routes.MapRoute( "Home", "{action}", new { controller = "Home" } );

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "home", action = "index", id = UrlParameter.Optional }
);

Now depending on which one is first I can get either one or the other to work, but not both.

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

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

发布评论

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

评论(2

各自安好 2024-10-17 01:38:01

我认为您可能正在寻找的是下面代码的作者称为根控制器的东西。我自己在几个网站上使用过这个,它确实可以创建漂亮的 URL,同时不需要您创建更多您想要的控制器,或者最终得到重复的 URL。

该路由位于 Global.asax 中:

        // Root Controller Based on: ASP.NET MVC root url’s with generic routing Posted by William on Sep 19, 2009
        //  http://www.wduffy.co.uk/blog/aspnet-mvc-root-urls-with-generic-routing/
        routes.MapRoute(
            "Root",
            "{action}/{id}",
            new { controller = "Root", action = "Index", id = UrlParameter.Optional },
            new { IsRootAction = new IsRootActionConstraint() }  // Route Constraint
        );

在其他地方定义了此路由:

    public class IsRootActionConstraint : IRouteConstraint
    {
        private Dictionary<string, Type> _controllers;

        public IsRootActionConstraint()
        {
            _controllers = Assembly
                                .GetCallingAssembly()
                                .GetTypes()
                                .Where(type => type.IsSubclassOf(typeof(Controller)))
                                .ToDictionary(key => key.Name.Replace("Controller", ""));
        }

        #region IRouteConstraint Members

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            string action=values["action"] as string;
            // Check for controller names
            return !_controllers.Keys.Contains(action);
        }

        #endregion
    }

RootActionContraint 允许您仍然拥有其他路由,并防止 RootController 操作隐藏任何控制器。

您还需要创建一个名为 Root 的控制器。这不是一个完整的实现。 在此处阅读原始文章

I think what you might be looking for is something that that the author of the code below has termed a Root Controller. I have used this myself on a couple sites, and it really makes for nice URLS, while not requiring you to create more controllers that you'd like to, or end up with duplicate URLs.

This route is in Global.asax:

        // Root Controller Based on: ASP.NET MVC root url’s with generic routing Posted by William on Sep 19, 2009
        //  http://www.wduffy.co.uk/blog/aspnet-mvc-root-urls-with-generic-routing/
        routes.MapRoute(
            "Root",
            "{action}/{id}",
            new { controller = "Root", action = "Index", id = UrlParameter.Optional },
            new { IsRootAction = new IsRootActionConstraint() }  // Route Constraint
        );

With this defined elsewhere:

    public class IsRootActionConstraint : IRouteConstraint
    {
        private Dictionary<string, Type> _controllers;

        public IsRootActionConstraint()
        {
            _controllers = Assembly
                                .GetCallingAssembly()
                                .GetTypes()
                                .Where(type => type.IsSubclassOf(typeof(Controller)))
                                .ToDictionary(key => key.Name.Replace("Controller", ""));
        }

        #region IRouteConstraint Members

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            string action=values["action"] as string;
            // Check for controller names
            return !_controllers.Keys.Contains(action);
        }

        #endregion
    }

The RootActionContraint alows you to still have other routes, and prevents the RootController actions from hiding any controllers.

You also need to create a controller called Root. This is not a complete implementation. Read the original article here

我的奇迹 2024-10-17 01:38:01

您是否尝试过:

 routes.MapRoute(
 "Home_About", 
 "About", 
 new { controller = "Home", action = "About" } );

 routes.MapRoute(
 "Default",
 "{controller}/{action}/{id}",
 new { controller = "home", action = "index", id = UrlParameter.Optional }
 );

产品仍应由默认路由处理,而第一个产品可以处理您的关于路由。

Have you tried:

 routes.MapRoute(
 "Home_About", 
 "About", 
 new { controller = "Home", action = "About" } );

 routes.MapRoute(
 "Default",
 "{controller}/{action}/{id}",
 new { controller = "home", action = "index", id = UrlParameter.Optional }
 );

Products should still be handled by the Default route, while the first one can handle your About route.

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