2008-08-15 119 views
13

我是新來的MVC(和ASP.Net路由)。我試圖將* .aspx映射到名爲PageController的控制器。ASP.Net MVC路由映射

routes.MapRoute(
    "Page", 
    "{name}.aspx", 
    new { controller = "Page", action = "Index", id = "" } 
); 

豈不上述地圖*的.aspx代碼PageController?當我運行這一點,並鍵入任何.aspx頁面中我得到以下錯誤:

The controller for path '/Page.aspx' could not be found or it does not implement the IController interface. Parameter name: controllerType

有什麼事我不是在這裏做什麼?

回答

6

I just answered my own question. I had the routes backwards (Default was above page).

是的,你必須把默認路由上述所有自定義路線。

So this brings up the next question... how does the "Default" route match (I assume they use regular expressions here) the "Page" route?

默認路由根據我們稱之爲Convention的配置而匹配。 Scott Guthrie在他的第一篇關於ASP.NET MVC的博客文章中解釋了這一點。我建議你通讀它和他的其他帖子。請記住,這些是基於第一個CTP發佈的,並且框架已更改。您還可以在Scott Hanselman的asp.net網站上找到ASP.NET MVC上的網絡投射。

0

不知道你的控制器的外觀,這個錯誤似乎指向它無法找到控制器的事實。創建了PageController類後,您是否繼承了Controller? PageController是否位於Controllers目錄中?

這是我在Global.asax.cs中

routes.MapRoute(
    "Page", 
    "{Page}.aspx", 
    new { controller = "Page", action = "Index", id = "" } 
); 

路線這裏是我的控制器,它位於Controllers文件夾:

using System.Web.Mvc; 

namespace MvcApplication1.Controllers 
{ 
    public class PageController : Controller 
    { 
     public void Index() 
     { 
      Response.Write("Page.aspx content."); 
     } 
    } 
} 
6

我剛纔已經回答我的問題。我有路線倒退(默認在頁面上方)。以下是正確的順序。因此,這帶來了下一個問題......「默認」路線如何匹配(我假設他們在這裏使用正則表達式)「頁面」路線?

routes.MapRoute(
      "Page", 
      "{Name}.aspx", 
      new { controller = "Page", action = "Display", id = "" } 
     ); 

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

在羅布科納的MVC店面screencasts之一,他遇到這個確切的問題。如果你有興趣,大約23分鐘。

0
public class AspxRouteConstraint : IRouteConstraint 
{ 
    #region IRouteConstraint Members 

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
    { 
     return values["aspx"].ToString().EndsWith(".aspx"); 
    } 

    #endregion 
} 

註冊路線爲所有的aspx

routes.MapRoute("all", 
       "{*aspx}",//catch all url 
       new { Controller = "Page", Action = "index" }, 
       new AspxRouteConstraint() //return true when the url is end with ".aspx" 
       ); 

,您可以通過MvcRouteVisualizer

測試路線