ASP.NET Mvc路由问题

hec6srdp  于 2023-02-06  发布在  .NET
关注(0)|答案(1)|浏览(237)

我一直在用Route Debugger测试一些选项,但没有运气。让我描述一下我正在尝试的:
我有所有的路线"翻译"如下(我需要从英语翻译成葡萄牙语,有时简化):

routes.MapRoute("Article.Create", "cms/artigo/criar",
                new { controller = "Article", action = "Create" });

一切正常。既然我已经定义了所有的路由,我想我不需要默认路由。所以我有这样的东西:

// Other translation routes
routes.MapRoute("Article.Create", "cms/artigo/criar",
                new { controller = "Article", action = "Create" });
routes.MapRoute("Article.Edit", "cms/artigo/editar/{id}",
                new { controller = "Article", action = "Edit", id = "" });
routes.MapRoute("Home.Index", "inicio",
                new { controller = "Home", action = "Index" });
routes.MapRoute("Home.Contact", "contacto",
                new { controller = "Home", action = "Contact" });

// Error Unknown
routes.MapRoute("Error.Unknown", "erro",
                new { controller = "Error", action = "Unknown" });

// Start
routes.MapRoute("Start", "_Default.aspx",
                new { controller = "Home", action = "Index" });

// Error NotFound
routes.MapRoute("Catch.All", "{*path}",
                new { controller = "Error", action = "NotFound", path = "" });

如果根目录经过转换Map但没有匹配,则如果是未知错误,则仅显示"http://domain.com/erro"
如果不是,则尝试匹配主页/索引。
最后将捕获所有路径,并仅显示路径,如下所示"http://domain.com/the/invalid/path
在我的Web.config上,我有:

<customErrors mode="On" defaultRedirect="~/Error/Unknown">
  <error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>

发生什么是当我开始我的网站,它总是去找不到错误,而不是开始页。
我哪里做错了?

qij5mzcb

qij5mzcb1#

您已定义指向主页的唯一URL为“~/yourapp/_Default.aspx”,但您的起始URL为“~/yourapp/",Catch.All路由将正确截获该URL。您必须将起始路由更改为:

MapRoute("Start", "", new { controller = "Home", action = "Index" });

顺便说一句:如果你想对你的路由进行单元测试,你可能会对以下内容感兴趣:Unit testing ASP.NET MVC routes. I提供了一个测试fixture来测试表格样式的路由-如果您有许多可能的路由(示例代码可用),那么它是理想的。

相关问题