代码之家  ›  专栏  ›  技术社区  ›  Ryan Eastabrook

Net MVC路由映射

  •  13
  • Ryan Eastabrook  · 技术社区  · 18 年前

    我是MVC(和ASP.Net路由)的新手。我在试着绘制地图 *.aspx PageController .

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

    上面的代码不会将*.aspx映射到 ? 当我运行此命令并键入任何.aspx页面时,会出现以下错误:

    参数名称:controllerType

    有什么我没在这里做的吗?

    5 回复  |  直到 7 年前
        1
  •  6
  •   Ryan Eastabrook    18 年前

    我只是回答了我自己的问题。我把路线倒过来了(默认设置在页面上方)。下面是正确的顺序。这就引出了下一个问题。。。“默认”路由如何与“页面”路由匹配(我假设他们在这里使用正则表达式)?

    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
            );
    
        2
  •  6
  •   Dale Ragan    18 年前

    我只是回答了我自己的问题。我有 向后路由(默认为

    是的,您必须将所有自定义路由置于默认路由之上。

    这就引出了下一个问题。。。 这里)是“页面”路线吗?

    默认路由匹配基于我们称之为配置之上的约定。Scott Guthrie在他关于ASP.NET MVC的第一篇博文中对此进行了很好的解释。我建议你通读这篇文章和他的其他文章。请记住,这些都是根据第一个CTP发布的,框架已经改变。您还可以在Scott Hanselman的ASP.NET网站上找到ASP.NET MVC上的网络广播。

        3
  •  1
  •   Chris Farmer Marcelo Cantos    18 年前

    在Rob Conry的MVC店面上 screencasts ,他遇到了这个问题。如果你感兴趣的话,大概在23分钟左右。

        4
  •  0
  •   Dale Ragan    18 年前

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

    这是我的控制器,位于控制器文件夹中:

    using System.Web.Mvc;
    
    namespace MvcApplication1.Controllers
    {
        public class PageController : Controller
        {
            public void Index()
            {
                Response.Write("Page.aspx content.");
            }
        }
    }
    
        5
  •  0
  •   Dayi Chen    15 年前
    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

    推荐文章