locked
Areas: _ViewImports.cshtml Configuration RRS feed

  • Question

  • User-1590999572 posted

    Visual Studio Code 1.48 | .NET Core 3.1

    Hi all

    According to the Official Microsoft Documentation about the use of Areas if it has the following structure:

    • Literature
      • Areas
        • Authors
          • Pages
            • Bibliography
            • Synopsis
            • Photos
        • Books
          • Pages
            • Genres
            • Catalogue
            • Photos

    Literature is the Name of the Project Root Folder.

    To create the Photos Page, (in the Pages Folder) both in the Authors Area and in the Books Area and prevent it from being considered as a Duplicate in the default Namespace (MyApp.Namespace), the _ViewImports.cshtml File must be implemented in the Pages Folder of each Area (in the example: Authors Folder and Books Folder) to recognize the "Local Namespace" with the following configuration:

    In the Authors Area:

          @namespace Literature.Areas.Authors.Pages
          @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    In the  Books Area:
          @namespace Literature.Areas.Books.Pages
          @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

    With the following final structure:

    • Literature
      • Areas
        • Authors
          • Pages
            • _ViewImports
            • Bibliography
            • Synopsis
            • Photos
        • Books
          • Pages
            • _ViewImports 
            • Genres
            • Catalogue
            • Photos

    However, when creating the Photos Pages with the dotnet command from the Terminal (remember that the editor is Visual Studio Code), it continues to configure the default Namespaces, like this

    In the Photos.cshtml Files:
          @page
          @model MyApp.Namespace.PhotosModel
          @ {
          }

    In the Photos.cshtml.cs Files:
          namespace MyApp.Namespace

    Then, given this configuration (which does not recognize the Local Files Configuration _ViewImports.cshtml of each Area) the error occurs:

          The namespace 'MyApp.Namespace' already contains a definition for 'PhotosModel'

    1 ° What is the Error or in any case how should the _ViewImports Files be configured correctly?

    2 ° To use the Areas feature, is it required that the Content Pages be located within the Pages Folder?

    Thank you very much already

    Wednesday, October 21, 2020 11:39 AM

All replies

  • User475983607 posted

    You can achieve the same folder/file organization by adding folders to Page folder.  This is one of the main features/benefits of Razor Pages.  The main reason to use Areas in Razor Page is to create Razor Class Libraries which does not seem to be the use case. 

    Wednesday, October 21, 2020 11:52 AM
  • User1312693872 posted

    Hi,Skar.2007

    1 ° What is the Error or in any case how should the _ViewImports Files be configured correctly?

    2 ° To use the Areas feature, is it required that the Content Pages be located within the Pages Folder?

    1, The _ViewImports contains the route to the Pages folder, I create the structure you has but nothing wrong happened. You should check your other _ViewImports files, if some duplicates exist, then your error will happen.

    2, Yes, you should better put it in Pages folder. Razor pages within an area must be placed in a folder named Pages.

    You can check the following link:

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-3.1#areas-with-razor-pages

    https://www.learnrazorpages.com/advanced/areas

    Best Regards,

    Jerry Cai

    Thursday, October 22, 2020 3:36 AM