2012-02-20 79 views
0

我正在研究ASP.NET MVC應用程序。出於某種原因,每當我認爲我理解路由時,就會彈出一些我不明白的東西。目前,我有兩條路線,我似乎無法弄清楚。我的目錄結構如下所示MVC路由誤解

- Views 
    - Internal 
    - Profile 
     - Index.cshtml 
    - Input 
     - Page1.cshtml 

在我的Global.asax.cs文件,我已經添加了以下映射:

routes.MapRoute(
    "UserProfileInfo", 
    "{controller}/profile", 
    new { controller = "Internal", action = "UserProfileInfo" } 
); 


    routes.MapRoute(
    "Page1", 
    "{controller}/input/page1", 
    new { controller = "Internal", action = "Page1" } 
); 

在myController的,我有以下幾點:

public ActionResult UserProfileInfo() 
    { 
    return View("~/Views/internal/profile/Index.cshtml"); 
    } 

    public ActionResult Page1() 
    { 
    return View("~/Views/internal/input/Page1.cshtml"); 
    } 

我想將我的操作存儲在單個控制器中。我以爲我已經正確設置了一切。但我繼續得到404.我做錯了什麼?

+0

什麼URL給你404? – Simon 2012-02-20 13:12:33

+0

當我衝在http:// localhost:[端口] /內部/輸入/ page1或http:// localhost:[端口] /配置文件/信息我得到一個404.我注意到,當我輸入http:// localhost :[port]/internal/UserProfileInfo我的頁面出現了。但是,這不是我想要使用的網址。 – 2012-02-20 13:40:59

+0

您可能需要刪除現有的默認路由,該路由可能會首先匹配 – Simon 2012-02-20 13:56:54

回答

0

從調用MapRoute的控制器名稱中刪除「Controller」後綴,以創建映射到名爲InternalController的類。在查找匹配的實現時,Controller後綴由框架附加。例如:

routes.MapRoute( 
    "UserProfileInfo", 
    "{controller}/profile", 
    new { controller = "Internal", action = "UserProfileInfo" } 
); 
+0

中爲您添加的{controller}/{action}/{id}路由,它們不在我的實際代碼中。就在這篇文章中。我已更新該帖子。謝謝你指出這一點。 – 2012-02-20 14:53:46