I have the latest versions of VS 2010, .NET 4.0 and MVC 3 and I have a problem using areas in asp.net mvc 3 when using vb.net.
I do the following:
At step 7 I get the message "The resource cannot be found".
If I do exactly the same thing using c# then I will get to the correct page and it will show the word "Index" as expected.
Is this a bug, or am I missing something? I have been scanning the web for hours trying to solve this but I am getting noware.
This is the automatically created TestAreaRegistration.vb file Namespace TestApp.Areas.Test Public Class TestAreaRegistration Inherits AreaRegistration
Public Overrides ReadOnly Property AreaName() As String
Get
Return "Test"
End Get
End Property
Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
context.MapRoute( _
"Test_default", _
"Test/{controller}/{action}/{id}", _
New With {.action = "Index", .id = UrlParameter.Optional} _
)
End Sub
End Class
End Namespace
And this is the automatically created Global.ascx file: ' Note: For instructions on enabling IIS6 or IIS7 classic mode, ' visit http://go.microsoft.com/?LinkId=9394802
Public Class MvcApplication Inherits System.Web.HttpApplication
Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
filters.Add(New HandleErrorAttribute())
End Sub
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
' MapRoute takes the following parameters, in order:
' (1) Route name
' (2) URL with parameters
' (3) Parameter defaults
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
)
End Sub
Sub Application_Start()
AreaRegistration.RegisterAllAreas()
RegisterGlobalFilters(GlobalFilters.Filters)
RegisterRoutes(RouteTable.Routes)
End Sub
End Class
These are identical with what you get if you repete steps 1-7 and use c# (only difference is that you will get c# code that is equal to above vb.net code).
I repete: If I do the steps 1-7 in C# it works, but it will not work in vb.net! What is wrong?
The problem is that your controllers are not in the default controller namespace. You will need to manually reference the controller namespaces yourself. There is an overload for this. Try this:
Public Overrides Sub RegisterArea(ByVal context As System.Web.Mvc.AreaRegistrationContext)
context.MapRoute( _
"Test_default", _
"Test/{controller}/{action}/{id}", _
New With {.action = "Index", .id = UrlParameter.Optional}, _
New With {"MyDefaultNamespace/Areas/Test/Controllers"} _
)
End Sub
The default area config file doesn't actually reference the area's controller namespace. This is an unfortunate oversight. So, unless you copy your old controllers over in to the new folders (and retain the old namespace), you'll get an error on the first go. referencing the correct namespaces will fix this issue.
edit:
Also, you didn't mention anything about creating an action for your page... is this a typo, or did the controller come with one by default?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With