Overriding/Changing Base Razor View Page Type in ASP.NET Core




                 Using razor views, sometimes we might need additional class properties/methods in all our views than we have in the existing built-in base razor view. For example, our all view pages might need title, keywords, etc. In ASP.NET Core, we can easily create a base razor view page class and set this class as a base razor view type for all our views. In this post, I will show you an example of this.    




                    Note that I am using beta7 at the time of writing. Razor views are inherit from generic Microsoft.AspNet.Mvc.Razor.RazorPage class. We can create our custom base razor view class by inheriting RazorPage class, 


    public abstract class MyRazorPage<T> : RazorPage<T>
        public string Title { get; set; }
        public string Description { get; set; }
        public string[] Keywords { get; set; }

                    We have added three more properties in our base view. Now for making this class as our base razor view globally, we can register it in _ViewImports.cshtml file,


         @inherits MyNameSpace.MyRazorPage<TModel>


                    Now we can use the new properties (Title, Description and Keywords) anywhere in our view. 




                    In this article, I showed you an example of how to create a base razor view page and then how to register this class globally.


  • Thanks for the most powerpul information . It Hub Online Training provides Dot Net online training Creating
    <a href=”http://www.ithubonlinetraining.com/dotnet-online-training/”>Dot Net Online Training</a>

  • Doh, I did exactly the same but with an outcome of 500 server error.
    This is the only article i can find that talks about it.

    "View of type 'Asp.ASPV__Views_Default_Index_cshtml' cannot be activated by 'Microsoft.AspNet.Mvc.Razor.RazorPageActivator'."

    I can only assume I'm missing some extra config to tell it my custom type is what i want to use.

  • Now a days every business man maintain one website that is static are dynamic websites.This is very useful.thanks for the explain use of this info<a href="http://www.ithubonlinetraining.com/dotnet-online-training/">dotnetonlinetraining</a>

  • This is very good information for knowing the dotnet basic knowledage <a href="http://www.ithubonlinetraining.com/dotnet-online-training/">dotnetonlinetraining</a>

Comments have been disabled for this content.