ASP.Net MVC route mapping

≡放荡痞女 提交于 2019-11-30 22:36:23

问题


I'm new to MVC (and ASP.Net routing). I'm trying to map *.aspx to a controller called PageController.

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

Wouldn't the code above map *.aspx to PageController? When I run this and type in any .aspx page I get the following error:

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

Is there something I'm not doing here?


回答1:


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

Yeah, you have to put all custom routes above the Default route.

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

The Default route matches based on what we call Convention over Configuration. Scott Guthrie explains it well in his first blog post on ASP.NET MVC. I recommend that you read through it and also his other posts. Keep in mind that these were posted based on the first CTP and the framework has changed. You can also find web cast on ASP.NET MVC on the asp.net site by Scott Hanselman.

  • http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
  • http://www.asp.net/MVC/



回答2:


I just answered my own question. I had the routes backwards (Default was above page). Below is the correct order. So this brings up the next question... how does the "Default" route match (I assume they use regular expressions here) the "Page" route?

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
        );



回答3:


On one of Rob Conery's MVC Storefront screencasts, he encounters this exact issue. It's at around the 23 minute mark if you're interested.




回答4:


Not sure how your controller looks, the error seems to be pointing to the fact that it can't find the controller. Did you inherit off of Controller after creating the PageController class? Is the PageController located in the Controllers directory?

Here is my route in the Global.asax.cs

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

Here is my controller, which is located in the Controllers folder:

using System.Web.Mvc;

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



回答5:


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
}

register the route for all aspx

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

And you can test the routes by MvcRouteVisualizer



来源:https://stackoverflow.com/questions/11926/asp-net-mvc-route-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!