Routing without naming controller

2019-08-31 13:35发布

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?

2条回答
女痞
2楼-- · 2019-08-31 14:10

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

routes.MapRoute(
  "MyTerrificRoute",
  "{action}",
  new { controller = "MyController", action = "Index" }
);
查看更多
Ridiculous、
3楼-- · 2019-08-31 14:14

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)
    {
        ...
    }
}
查看更多
登录 后发表回答