0

我正在尝试执行以下操作但没有成功:

网址:

MyString/MyController1
MyString/MyController2
MyString/MyController3
MyString/

对于我使用的前三个:

routes.MapRoute(
                "MyString/MyController1", // Route name
                "MyString/MyController1/{action}/{id}", // URL with parameters
                new {controller = "MyController1", action = "Index", id = UrlParameter.Optional},
                new string[] {"Core_Web.Controllers.MyString"}
                );

它工作得很好。当我尝试访问MyString/时,我的问题就来了。我已经添加了 :

routes.MapRoute(
                "Default",
                "MyString",
                new { controller = "MyString", action = "Index", id = UrlParameter.Optional },
                new string[] {"Core_Web.Controllers"}
                );

但是 URL MyString的调用给出了:

没有找到您要查的资源

说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。

请求的 URL:/MyString

有一个控制器名为MyString.Core_Web.Controllers

感谢您的帮助,

[编辑]

这是我的整个代码:

路线:

routes.MapRoute(
    "Contabilita",
    "Contabilita",
    new { controller = "Contabilita", action = "Index" },
    new string[] { "Core_Web.Controllers" }
    );

routes.MapRoute(
    "Contabilita_Controller", // Route name
    "Contabilita/{controller}/{action}/{id}", // URL with parameters
    new { action = "Index", id = UrlParameter.Optional},
    new string[] { "Core_Web.Controllers.Contabilita" }
    );

和控制器:

namespace Core_Web.Controllers
{
    public class ControllerContabilitaBase : ControllerBaseSuper
    {
       ....
    }
    public class ControllerContabilita : ControllerContabilitaBase
    {
        public ActionResult Index()
        {
            return View(GetView("Index"));
        }
    }
}

namespace Core_Web.Controllers.Contabilita
{
    public class ImpiantoController : ControllerContabilitaBase
    {
     ...
    }
    public class LottoController : ControllerContabilitaBase
    {
    ...
    }
}

但它也不起作用。带有控制器的网址有效,但另一个无效。它给出了 404。

我可能忽略了别的东西吗?

[/编辑]

4

1 回答 1

1

Try the following route definitions (in that order):

routes.MapRoute(
    "MyString",
    "MyString",
    new { controller = "MyString", action = "Index" },
    new string[] { "Core_Web.Controllers" }
);

routes.MapRoute(
    "MyString_Controller",
    "MyString/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    new string[] { "Core_Web.Controllers" }
);

Which should give you the following pattern:

url                  controller             action
--------------------------------------------------
/mystring            MyStringController     Index
/mystring/my1        My1Controller          Index
/mystring/my1/foo    My1Controller          Foo
/mystring/my2        My2Controller          Index
/mystring/my3        My3Controller          Index
于 2012-07-17T13:44:09.633 回答