2016-08-08 43 views
0

我正在開發一個簡單的CRUD應用程序使用ASP MVC 1,將爲多個學生程序跨多個表存儲數據。在這樣做的過程中,我試圖弄清楚如何構建URL以適應每個程序,它們的表格和它們的操作。ASP MVC 1自定義路由的文件夾和子文件夾

例如,這是我想實現的是:

site.com/StudProg1/Participant/Create將是URL方案1

插入了用戶表中的條目爲學生site.com/StudProg2/Course/將作爲學生計劃2的課程表的索引頁的URL

在我嘗試創建自定義路由以適應此情況時,我的Global.asax.cs文件表示爲如下:

public class MvcApplication : System.Web.HttpApplication 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "StudProg1", 
      "StudProg1/{pageName}/{action}", 
      new { controller = "StudProg1", pageName="Index", action = "Index" } 
     ); 

     routes.MapRoute(
      "StudProg2", 
      "StudProg2/{pageName}/{action}", 
      new { controller = "StudProg2", pageName="Index", action = "Index" } 
     ); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 
    } 
} 

其中pageName應該是表名。毫不奇怪,上述結構不會返回我所瞄準的輸出(即它總是指向StudProg的Index頁面)。

此外,我的文件目錄結構是這樣:

 
Views 
    StudProg1 
     Index 
     Participant 
      Index 
      Create 
    StudProg2 
     Index 
     Courses 
      Index 
      Create 

我的問題是,我如何能提高我的路線正確實現URL結構我的願望。此外,除了Microsoft ASP.NET開發人員網絡站點之外,有沒有關於自定義路由的好教程?

回答

0

你不應該爲每個學生程序(StudProg1,StudProg2等)創建一個控制器。你可以接受這個參數。

您只需要2個控制器,一個用於參與者,另一個用於課程。

public class ParticipantController : Controller 
{ 
    public ActionResult Index() 
    { 
     return Content("Reques for partiicpant index page if needed"); 
    } 
    public ActionResult create(string studentProgram) 
    { 
     return Content("Partiicpant create for "+studentProgram); 
    } 
} 
public class CourseController : Controller 
{ 
    public ActionResult Index() 
    { 
     return Content("Course:"); 
    } 
} 

,並在您RouteConfig的方法的RegisterRoutes,指定通用默認路由前2條具體路線。

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute("part","{studentProgram}/Participant/{action}", 
     new { controller = "Participant", action = "Index" } 
    ); 

    routes.MapRoute("course", "{studentProgram}/Course/{action}", 
         new { controller = "Course", action = "Index" } 
    ); 

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

}