【问题标题】:How to configure a URL with 3 levels in ASP.NET MVC?如何在 ASP.NET MVC 中配置 3 个级别的 URL?
【发布时间】:2011-01-26 23:01:38
【问题描述】:

使用 ASP.NET MVC,我需要像这样配置我的 URL:

www.foo.com/company:渲染查看公司

www.foo.com/company/about : 渲染查看公司

www.foo.com/company/about/mission : 渲染查看任务

如果“公司”是我的控制者,“关于”是我的行动,那么“使命”应该是什么?

对于每个“文件夹”(公司、关于和任务),我必须呈现不同的视图。

有人知道我该怎么做吗?

谢谢!

【问题讨论】:

    标签: asp.net asp.net-mvc routing url-routing


    【解决方案1】:

    首先,设置您的视图:

    Views\
      Company\
        Index.aspx
        About.aspx
        Mission.aspx
        AnotherAction.aspx
    

    在您的 GlobalAsax.RegisterRoutes(RouteCollection routes) 方法中:

    public static void RegisterRoutes(RouteCollection routes)
    {
      // this will match urls starting with company/about, and then will call the particular
      // action (if it exists)
      routes.MapRoute("mission", "company/about/{action}",
            new { controller = "Company"});
      // the default route goes at the end...
      routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
      );
    }
    

    在控制器中:

    CompanyController
    {
      public ViewResult Index() { return View(); }
      public ViewResult About() { return View(); }
      public ViewResult Mission() { return View(); }
      public ViewResult AnotherAction() { return View(); }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-28
      • 2012-09-19
      • 1970-01-01
      • 2012-01-03
      • 2023-03-12
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多