Routing without naming controller

2019-08-31 13:49发布

问题:

Is it possible to have route like this : www.MyDomin/AnyPage www.MyDomin/AnyPage1 and so on ? Instead of www.MySite/MyController/AnyPage

Without specifying the controller?

回答1:

Well you could define the following route:

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

Now all that's left is have the controller action to which this would map:

public class HomeController: Controller
{
    public ActionResult Index(string page)
    {
        ...
    }
}


回答2:

Yes it is. Edit your routes in the Global.asax file, for example:

routes.MapRoute(
  "MyTerrificRoute",
  "{action}",
  new { controller = "MyController", action = "Index" }
);