locked
How to use a Custom PageBaseModel class in Razor Pages RRS feed

  • Question

  • User-1042970710 posted

    Dear Friends,

    I have created a  custom  class inherited from the Razor Page PageModel.  In my custom class I am  injecting other services which I have defined in my Startup.CS file, I  did this becuase I want to avoid injecting in  all the pages I will create later on...

    Here is the code:

     public class BasePageModel : PageModel
        {
            public IConfiguration Configuration { get; }
            public BasePageModel(IConfiguration configuration)
            {
                this.Configuration = configuration;
            }
    
        }

    Now my question is when I create new Razor Page  I will  inherit page from my custom class  (BasePageModel )rather then PageModel  class,

     How I can send IConfiguration object when inheriting from BasePageModel ??   

    What is the correct syntax?  

    // ???

    public class TestSettingsModel : BasePageModel(IConfiguration configuration) { public void OnGet() { } }

    Is there any better approach then this?

    kindly help.

     

    Wednesday, October 21, 2020 9:07 AM

Answers

  • User475983607 posted

    You can get the service from the current context.

        public class BasePageModel<T> : PageModel where T: BasePageModel<T>
        {
            private readonly IConfiguration _configuration;
            protected IConfiguration Configuration => _configuration ?? HttpContext.RequestServices.GetService<IConfiguration>();
    
            private readonly ILogger<T> _logger;
            protected ILogger<T> Logger => _logger ?? HttpContext.RequestServices.GetService<ILogger<T>>();
    
        }

    Implementation

    using Microsoft.Extensions.Logging;
    using RazorDemo.Configuration;
    
    namespace RazorDemo.Pages
    {
        public class IndexModel : BasePageModel<IndexModel>
        {
            public IndexModel() 
            {
    
            }
    
            public void OnGet()
            {
                config = this.Configuration["Logging:LogLevel:Default"];
                this.Logger.LogInformation("this is an informative log");
            }
    
            public string config;
        }
    }
    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    
    <div class="text-center">
        @Model.config
    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 21, 2020 12:59 PM

All replies

  • User475983607 posted

    The syntax to invoke the base constructor is...

        public class IndexModel : BasePageModel
        {
            private readonly ILogger<IndexModel> _logger;
    
            public IndexModel(ILogger<IndexModel> logger, IConfiguration configuration) 
                :base(configuration)
            {
                _logger = logger;
            }
    
            public void OnGet()
            {
    
            }
        }

    C# programming guide.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors

    Wednesday, October 21, 2020 10:54 AM
  • User-1042970710 posted

    Thank you very much for your reply. 

    There is one problem,  I still have to declare the service in my page, which I want to avoid.  I have around 4 services injected in startup.cs

    Actually, The main reason to create Base class is to avoid injecting the services to every new page I create .  I wanted to leverage the inheritance feature of C#   

    I found this link 

    https://stackoverflow.com/questions/54783856/basepagemodel-in-razor-pages

    but this is also not an elegant solution, but I think I go with that if don't find anything else

    Wednesday, October 21, 2020 11:54 AM
  • User475983607 posted

    You can get the service from the current context.

        public class BasePageModel<T> : PageModel where T: BasePageModel<T>
        {
            private readonly IConfiguration _configuration;
            protected IConfiguration Configuration => _configuration ?? HttpContext.RequestServices.GetService<IConfiguration>();
    
            private readonly ILogger<T> _logger;
            protected ILogger<T> Logger => _logger ?? HttpContext.RequestServices.GetService<ILogger<T>>();
    
        }

    Implementation

    using Microsoft.Extensions.Logging;
    using RazorDemo.Configuration;
    
    namespace RazorDemo.Pages
    {
        public class IndexModel : BasePageModel<IndexModel>
        {
            public IndexModel() 
            {
    
            }
    
            public void OnGet()
            {
                config = this.Configuration["Logging:LogLevel:Default"];
                this.Logger.LogInformation("this is an informative log");
            }
    
            public string config;
        }
    }
    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    
    <div class="text-center">
        @Model.config
    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 21, 2020 12:59 PM