How do I configure ASP.NET MVC routing to hide the controller name on a “home” page?

杀马特。学长 韩版系。学妹 提交于 2019-12-17 07:34:27

问题


Following on from this question:

ASP.NET MVC Routing with Default Controller

I have a similar requirement where my end user doesn't want to see the controller name in the url for the landing or "home page" for their application.

I have a controller called DeviceController which I want to be the "home page" controller. This controller has a number of actions and I'd like to use URL's like the following:

http://example.com  -> calls Index()  
http://example.com/showdevice/1234 -> calls ShowDevice(int id)  
http://example.com/showhistory/1224 -> calls ShowHistory(int id)  

I also need links generated for this controller to leave out the /device part of the url.

I also have a number of other controllers, for example BuildController:

http://example.com/build  
http://example.com/build/status/1234  
http://example.com/build/restart/1234  

and so on. The URL's for these controllers are fine as they are.

The problem is that I just can't seem to get my head around the routing for this even after studying the answers to the question referenced above.

Can someone provide a code sample explaining how to do this?

I'm using ASP.NET MVC2.


回答1:


Try this:

   private void RegisterRoutes(RouteCollection routes) {

      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapRoute("default", "{controller}/{action}/{id}",
         new { action = "index", id = "" },
         // Register below the name of all the other controllers
         new { controller = @"^(account|support)$" });

      routes.MapRoute("home", "{action}",
         new { controller = "device", action = "index" });
   }

e.g. /foo

If foo is not a controller then it's treated as an action of the device controller.




回答2:


Step 1: Create the route constraint.

public class RootRouteConstraint<T> : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var rootMethodNames = typeof(T).GetMethods().Select(x => x.Name.ToLower());
        return rootMethodNames.Contains(values["action"].ToString().ToLower());
    }
}

Step 2:
Add a new route mapping above your default mapping that uses the route constraint that we just created. The generic parameter should be the controller class you plan to use as your “Root” controller.

routes.MapRoute(
                "Root",
                "{action}",
                new {controller = "Home", action = "Index", id = UrlParameter.Optional},
                new {isMethodInHomeController = new RootRouteConstraint<HomeController>()}
                );


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

Now you should be able to access your home controller methods like so: example.com/about, example.com/contact

This will only affects the url of HomeController. Alll other Controllers will have the default routing functionality.



来源:https://stackoverflow.com/questions/3627984/how-do-i-configure-asp-net-mvc-routing-to-hide-the-controller-name-on-a-home-p

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