0

我正在Web站點項目(而不是Web應用程序)中工作,Web窗體和MVC愉快地生活在一起。爲了組織我的代碼,我試圖設置具有MVC部分的區域,並且正在遇到這種情況。在RouteConfig中工作的相同路徑,但不是AreaRegistration

我設置我與控制器區域和我創建以下區域配置:

Namespace Areas.Awesome 

    Public Class AwesomeAreaRegistration 
     Inherits AreaRegistration 

     Public Overrides ReadOnly Property AreaName As String 
      Get 
       Return "Awesome" 
      End Get 
     End Property 

     Public Overrides Sub RegisterArea(context As AreaRegistrationContext) 

      context.MapRoute(
       "Awesome_default", 
       "Awesome/{controller}/{action}/{id}", 
       New With {.controller = "Sauce", .action = "Index", .id = UrlParameter.Optional}, 
       New String() {"Areas.Awesome"} 
      ) 

     End Sub 

    End Class 

End Namespace 

當我嘗試導航到/Awesome/Sauce/我得到一個404錯誤,我的網站實際上是嘗試的路線我/Awesome/Sauce/Default.aspx

然而,當我移動的路線我RouteConfig

Public Module RouteConfig 

    Public Sub RegisterRoutes(ByVal routes As RouteCollection) 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}") 
     routes.IgnoreRoute("{resource}.aspx/{*pathInfo}") 

     routes.MapRoute(
      "Awesome_default", 
      "Awesome/{controller}/{action}/{id}", 
      New With {.controller = "Sauce", .action = "Index", .id = UrlParameter.Optional}, 
      New String() {"Areas.Awesome"} 
     ) 
    End Sub 

End Module 

這起到了/Awesome/Sauce/預期。

我做了一些挖掘並同時創建了兩個路由,但使用不同的URI,我發現它們都以相同的方式定義,但一個正在工作,另一個不是。

有什麼我失蹤的區域註冊會導致這些路由被忽略,而在RouteConfig中定義的路由不是?

回答

0

它可能與名稱空間應用於該區域的方式一致。

this article

命名空間可能只是不適合該地區完全合格。

public class ContactsAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get 
     { 
      return "Contacts"; 
     } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      "Contacts_default", 
      "Contacts/{controller}/{action}/{id}", 
      new { action = "Index", id = UrlParameter.Optional }, 
      namespaces: new[] { "MvcApplication1.Areas.Contacts.Controllers" } 
     ); 
    } 
} 
相關問題