Monday, 20 July 2015

Create a Custom ViewResult Return type to return a View in MVC application

In this post we are going to see how to create the Custom ViewResult, which can return the view of corresponding action for a controller.

First we have to derive a class from ViewResultBase from which we can create a custom viewResult.


    public class CustomViewResult:ViewResultBase
    {
        private string _master;
        public string Master
        {
            set { _master = value; }
            get { return this._master ?? string.Empty; }
        }

        protected override ViewEngineResult FindView(ControllerContext context)
        {
            ViewEngineResult _viewEngine = base.ViewEngineCollection.FindView(context,      
                                           ViewName, Master);
            if (_viewEngine.View != null)
                return _viewEngine;

            StringBuilder builder = new StringBuilder();

            foreach (var item in _viewEngine.SearchedLocations)
            {
                builder.Append(Environment.NewLine);
                builder.Append(item);
            }

            throw new                
            InvalidOperationException(string.Format(CultureInfo.CurrentCulture,ErrorData,
                     new []{base.ViewName,builder.ToString()}));
        }

        public string ErrorData {
            get
            {
                return "{0} ~ View not found in the following Locations {1}";
            }
        }
    }


Now create a class which is derived from the controller class, where we have to specify the wrapper method to create a instance for CustomView along with some overload methods.

   public class CustomController:Controller
   {
        protected internal CustomViewResult CustomView() {           
            return this.CustomView(null, null, null);
        }

        protected internal CustomViewResult CustomView(string viewname) {
            return this.CustomView(viewname, null, null);
        }

        protected internal CustomViewResult CustomView(string viewname, object model) {
            return this.CustomView(viewname, null, model);
        }

        protected internal virtual CustomViewResult CustomView(string viewname,string  
                                                                master,object model)
        {

            if (model != null)
                base.ViewData.Model = model;

            return new CustomViewResult() {
              ViewName = viewname,
              Master = master,
              TempData = base.TempData,
              ViewData = base.ViewData,
              ViewEngineCollection = base.ViewEngineCollection
            };
       
        }
    }

After this two class creations, now derive the controller class from CustomController, In my sample application controller name is HomeController

For example:

    public class HomeController : CustomController
    {
        public ActionResult Index()
        {
            return CustomView();
        }
    }



When we invoke the HomeController Index action, instead of View our CustomView is executed and returned the view to the client Browser.





After Button click 



For Example if we pass a view name in the custom view it will render, but if we pass a wrong view name, then the error message that we mentioned will be shown 

For example : we are passing the view name as SampleView , but it not exists, Let we see what is the output.




 we are printing the searched location where viewEngine Searched.

From this post you can learn how to create a custom ViewResult  which can return a generated view to the users.

No comments:

Post a Comment