Asp.NET MVC路由问题:找不到资源

本文关键字:找不到 资源 问题 路由 NET MVC Asp | 更新日期: 2024-10-18 20:23:16

我的mvc web项目有以下设置

/Area
  Admin
    HomeController
  Customer
    HomeController
    YearController
/Controllers
  AgentsController
  EmployeeController

我想将客户区域的主控器的路线更改为以下

http://mywebsite/Customer/{action}/{id}

我还希望所有其他路由都以默认方式运行。http://mywebsite/{area}/{controller}/{index}/{id}http://mywebsite/{controller}/{index}/{id}

我转到我的CustomerAreaRegistration,并将下面的代码添加到RegisterArea方法中,但它不起作用。当我导航到http://mywebsite/Customer/Createhttp://mywebsite/Agents/View时,它会正确显示页面。但如果我尝试导航到http://mywebsite/Customer/Year/Edit?yearId=3,它会显示找不到资源。

这是我的CustomerAreaRegistration 的注册区域方法

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRouteLowercase(
           "MyCustomerHomeDefault",
           "Customer/{action}/{id}",
           new { controller = "Home", action = "Index", id = UrlParameter.Optional }
       );
        context.MapRouteLowercase(
          "Customer_default",
          "Customer/{controller}/{action}/{id}",
          new { action = "Index", id = UrlParameter.Optional }
        );
    }

我没有对我的路线做任何其他更改,所以我不知道下一步该怎么办。我下载了路由调试器,它说与http://mywebsite/Customer/Year/Edit?yearId=3匹配的路由是

Matched Route: Customer/{action}/{id}
Route Data
Key         Value
action      year 
id          edit 
area        Customer
controller  Home 

有人能帮我了解如何解决这个问题吗?

Asp.NET MVC路由问题:找不到资源

由于路由条目是按照输入注册的顺序进行评估的,因此交换路由条目,以便先检查更具体的路由,然后再检查更一般的路由,如下所示:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRouteLowercase(
        "Customer_default",
        "Customer/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
    context.MapRouteLowercase(
        "MyCustomerHomeDefault",
        "Customer/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}