C# ASP.Net MVC 路由映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 17:41:23  来源:igfitidea点击:

ASP.Net MVC route mapping

提问by Ryan Eastabrook

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

我是 MVC(和 ASP.Net 路由)的新手。我正在尝试映射*.aspx到一个名为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:

上面的代码不会将 *.aspx 映射到PageController吗?当我运行它并输入任何 .aspx 页面时,我收到以下错误:

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

找不到路径“/Page.aspx”的控制器,或者它没有实现 IController 接口。参数名称:controllerType

Is there something I'm not doing here?

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

采纳答案by Dale Ragan

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.

默认路由根据我们所说的约定优于配置进行匹配。Scott Guthrie 在他关于 ASP.NET MVC 的第一篇博客文章中很好地解释了这一点。我建议您通读它以及他的其他帖子。请记住,这些是基于第一个 CTP 发布的,并且框架已更改。您还可以在 Scott Hanselman 的 asp.net 站点上找到有关 ASP.NET MVC 的网络广播。

回答by Dale Ragan

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?

不确定您的控制器的外观,错误似乎指向它找不到控制器的事实。创建 PageController 类后,您是否继承了 Controller?PageController 是否位于 Controllers 目录中?

Here is my route in the Global.asax.cs

这是我在 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:

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

using System.Web.Mvc;

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

回答by Ryan Eastabrook

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

回答by Chris Farmer

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.

在 Rob Conery 的其中一个 MVC Storefront截屏视频中,他遇到了这个确切的问题。如果您有兴趣,它大约在 23 分钟标记处。

回答by Dayi Chen

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

为所有 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

您可以通过MvcRouteVisualizer测试路线