Having trouble with a simple MVC route

你说的曾经没有我的故事 提交于 2019-11-30 19:03:56

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4571970/having-trouble-with-a-simple-mvc-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!