Add namespace to all views in ASP.NET MVC 6

后端 未结 3 1055
悲哀的现实
悲哀的现实 2021-02-18 13:56

I’m using MVC 6 and would like to be able to access a particular namespace globally from all of my Razor views. In MVC 5 this was fairly simple; I’d just add the following code

相关标签:
3条回答
  • 2021-02-18 14:17

    Add your namespaces to the_ViewImports.cshtml file (it's under the Views folder).

    Example file:

    @using Microsoft.AspNetCore.Identity
    @using Jifiti.Registry.Web.Models.AccountViewModels
    @using Jifiti.Registry.Web.Models.ManageViewModels
    
    @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    
    0 讨论(0)
  • 2021-02-18 14:19

    For <= beta3 bits (what you're most likely using) you should add an @using statements to your _ViewStart.cshtml. Aka:

    _ViewStart.cshtml: @using MyProject.WebUI.Helpers

    If you don't have a _ViewStart.cshtml you can create one and just make sure it's in the same path or parent path of the view you want it to affect.

    For beta4 bits, this functionality was moved to a new file called _GlobalImport.cshtml; _ViewStart.cshtml was transitioned back to its original functionality (just running code, not inheriting directives). Therefore:

    _GlobalImport.cshtml: @using MyProject.WebUI.Helpers

    For beta5 bits, _GlobalImport.cshtml was renamed to _ViewImports.cshtml

    0 讨论(0)
  • 2021-02-18 14:27

    I'm on beta 7 and I had to use

    @using System.Security.Principal
    @using System.Security.Claims
    
    @Context.User.GetUserId()
    
    0 讨论(0)
提交回复
热议问题