【问题标题】:Routing without naming controller没有命名控制器的路由
【发布时间】:2012-02-08 13:43:18
【问题描述】:

是否有可能有这样的路线: www.MyDomin/AnyPage www.MyDomin/AnyPage1 等等?而不是 www.MySite/MyController/AnyPage

不指定控制器?

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    你可以定义以下路线:

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

    现在剩下的就是要映射到的控制器操作:

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

    【讨论】:

      【解决方案2】:

      是的。在 Global.asax 文件中编辑您的路线,例如:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-07
        • 2015-07-20
        • 1970-01-01
        相关资源
        最近更新 更多