ActionResult vs. View() vs. ViewResult :- MVC interview questions with answers

In case you are new to MVC you can start from this MVC training series videos http://www.youtube.com/watch?v=KAKxm4eQP24 created by www.questpond.com

This is one of the most common interview question asked during MVC interviews.

Let’s take a look at how our MVC action method looks like.

publicActionResultMyAction()
{            
      return View();
}

In above action method as you can see, return type is ActionResult but we are returning the return value of view() function.

View() function
As we already know our controllers are basically derived from “Controller” base class.
It contain a method called view().
Basically this function

  1. Finds a folder called [Controller Name] in Views Folder
  2. Then try to look for either aspx,asxc or cshtml page with name “MyAction” inside above folder.
  3. Create ViewReusult of obtained view and then returns. 

If view function returns ViewResults then why our action method return type is ActionResult?
It’s because ViewResult class internally inherits from ViewResultBase class which in turn inherited from ActionResult class.
We return ActionResult from Action methods because it gives us the power to return instance of any class derived from ActionResult.
Example –

publicActionResult MyAction(int id)
{            
     if(id>10)
            return View();
     else
            returnnewHttpNotFoundResult();
}

Hope all of you have enjoyed reading this article.

Get live trainings on MVC,WCF,Design patterns, MSBI,WPF with live example. For detail visit www.Sukesh-Marla.com

 




   

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

One Response to ActionResult vs. View() vs. ViewResult :- MVC interview questions with answers

  1. Johne368 says:

    hi!,I really like your writing so so much! proportion we communicate more about your post on AOL? I need a specialist on this space to unravel my problem. Maybe that’s you! Looking ahead to see you. edekceacadfb

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s