Cannot load view in area in asp.net mvc 3 using vb

2019-06-18 17:00发布

问题:

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:

  1. Create a new visual basic asp.net mvc 3 project. Select Razor as the "View Engin" and call the project "TestApp".
  2. Create a new area called "Test", it will be in the folder: /Areas/Test.
  3. Add a new empty controller called "PageController.vb" in /Areas/Test/Controllers/".
  4. Add a new folder in /Areas/Test/Views/ called "Page".
  5. Add a new Empty View called "Index.vbhtml" in /Areas/Test/Views/Page.
  6. Run the project.
  7. Manually type the url "http://localhost:xyz/Test/Page" in the browser, where xyz is the automatically added portnumber generated by VS.

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?

回答1:

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?