2008-08-15 123 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

测试路线