在Areas\Blog\Controllers下新增BlogController及ShowRecent 和 ShowArchive兩個Action及View。
在Areas\Dashboard\Controllers下新增DashboardController及AddPost 和 EditPost兩個Action及View。
在Views\Shared\Site.Master,修改
<ul id="menu">
<li><%=Html.ActionLink("Home", "Index", "Home", New With {.area = ""}, Nothing)%></li>
<li><%=Html.ActionLink("Blog", "ShowRecent", "Blog", New With {.area = "Blog"}, Nothing)%></li>
<li><%=Html.ActionLink("Dashboard", "AddPost", "Dashboard", New With {.area = "Dashboard"}, Nothing)%></li>
<li><%=Html.ActionLink("About", "About", "Home", New With {.area = ""}, Nothing)%></li>
</ul>
點擊Blog產生錯誤訊息
點擊Dashboard產生錯誤訊息
我重開一個MVC 2的專案測試,Areas的使用很正常。我又換了台電腦,開一個MVC 3專案(VB),一模一樣的內容,在MVC 3的專案的Areas就是會產生以上的錯誤訊息,但C# MVC 3專執行很正常。 我知道Areas要注意命名空間,但不管我在 Global.asax 或 Areas\Blog\BlogAreaRegistration.vb 裡修改 MapRoute 命名空間,還是一樣的錯誤訊息。 我們來看BlogAreaRegistration.vb,這是你新增Areas時,系統自己產生的檔案,用意是在註冊路由。
Namespace Mvc3Area.Areas.Blog
Public Class BlogAreaRegistration
Inherits AreaRegistration
Public Overrides ReadOnly Property AreaName() As String
Get
Return "Blog"
End Get
End Property
Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
context.MapRoute( _
"Blog_default", _
"Blog/{controller}/{action}/{id}", _
New With {.action = "Index", .id = UrlParameter.Optional} _
)
End Sub
End Class
End Namespace
其實C#的Code差不多,以下為C#的程式碼。
using System.Web.Mvc;
namespace Mvc3AreasC.Areas.Blog
{
public class BlogAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Blog";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Blog_default",
"Blog/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
}
我中斷點設定 context.MapRoute 這一段,然後發現原來是Visual Basic 的 Namespace 有問題。如果我把 BlogAreaRegistration.vb 裡的 Namespace 註解前後來比較:
註解前:Mvc3Area.Mvc3Area.Areas.Blog.*
註解後:Mvc3Area.*
我們可以試著在根目錄Controllers產生一個 UserController.vb ,發現他的 Namespace 為 「Mvc3Area」,即專案名稱。也就是說,在MVC 3使用Visual Basic所產生 *AreaRegistration.vb 的是不正確的,您必須將專案名稱後的命名全部去除,即可讓 Areas 正常運作。
' 修改Mvc3Area.Areas.Blog
Namespace Mvc3Area
' 其他不用修改
End Namespace
修正命名空間後,您的 Areas 程式應該就能正常運作了。


確定是Bug了。
回覆刪除http://forums.asp.net/p/1708997/4555632.aspx/1?Re+Mvc3+Areas+Can+t+Running+VB+Only