How to Add/Remove View Engines in ASP.NET MVC


Here we will learn how to add/remove view engines in Asp.Net MVC.

Points covered:
1. Displaying the total available views
2. Clearing all the views
3. Adding the required views

Reading all the available view engines
using System.Web.Mvc;
 
namespace MVC.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public string Index()
        {
            string viewEngines = string.Empty;
            foreach (var item in ViewEngines.Engines)
            {
                viewEngines += item + "</br>";
            }
            return viewEngines;
        }
    }
}
Clearing all available view engines and adding required
using System.Web.Mvc;
using System.Web.Routing;
 
namespace MVC
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new RazorViewEngine());
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

Happy coding..!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s