13 August 2014

ViewResult, FileResult, RedirectResult, PartialViewResult

ViewResult

It renders a view as a web page. It can make use of view engines aspx and Razor. The actions of return type ViewResult always returns a view. MVC supports following view types:
1. ASPX
2. ASCX
3. VBHTML
4. CSHTML

Note: A controller can have more than one view of various types. That is, ASPX, ASCX, CSHTML and VBHTML. The priority is always given to ASPX. However, you can access a view by using following example:

public ViewResult Details()
{
   return View("~/Views/Employee/Details.cshtml");

FileResult

The file result returns binary output to write the content of a file to response. That is, it can return the content of any specific file in the browser. If the MIME type of the file is not supported then, it downloads the file.
Example:

public FileResult ReadDoc()
{
    return File(@"F:\wcf.pdf","application/pdf");
}

RedirectResult

It is similar to response.redirect method that can be used to redirect to specific URL.
Example:
Public RedirectResult Google()
{
   return Redirect("http://www.google.com");
}
RedirectToRouteResult

It is similar to server.transfer method that can be used to redirect to any controller  action within the application.
Example:
Public RedirectToRouteResult EmpDetails()
{
   return RedirectToAction("Details","Employee");
}

Redirect using Hyperlink: MVC provides Html Helper "Html.ActionLink", that renders as "" tag to redirect to any location.
Syntax:
@Html.ActionLink("LinkText","Action","Controller", new {html attributes})

Example:
@Html.ActionLink("Go to Employee","Details","Employee", new {target="_blank"},null)

null at the end is used to avoid unwanted default query string added automatically.


PartialViewResult

Partial view are like user controls in web forms. the intention of partial views is to create a prototype that can be accessed from any view.

Example:
* Create partial views
Public PartialViewResult Register()
{
   return PartialView();
}
Public PartialViewResult Login()
{
   return PartialView();
}
* Add partial view to a view
@Html.Partial("Register")



@Html.Partial("Login")

Note: Views are generated as Login.cshtml and Register.cshtml in case of Razor view engine. But, in case with ASPX view engine, these views are created with extension .ASCX.

Note: You can directly invoke partial views by a URL request. You can restrict them by using the action filter "ChildActionOnly".
Example:

[ChildActionOnly]
public PartialViewResult Register()
{
   return PartialView();
}

Action Results in MVC

1. The controller action are public and can be invoked by an URL request.
2. The controller actions can return a view, file etc. These return types are referred as action results.
3. The action results in MVC are defined under the base class 'ActionResult'.
4. The various types of action results and their helper methods are defined below:

Action Results
Helper Method
ViewResult
View()
PartialViewResult
PartialView()
RedirectResult
Redirect()
RedirectToRouteResult
RedirectToAction()
ContentResult
Content()
JsonResult
Json()
JavascriptResult
Javascript()
FileResult
File()
EmptyResult
None

11 August 2014

Rules to remember while creating MVC - Controller's action method

1. Action methods must be public
2. Action methods can't be static
3. Action methods can be overloaded
4. Action methods can't be overridden
5. All Controllers are inherited from Controller class, which has it's own action methods. New controller's action method can't have same action method name that parent class has.
6. Action method parameters can't be 'ref' or 'out'.
7. Actions can't be extension methods.
7. All actions are invoked by 'Get' request.