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:
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:
RedirectResult
It is similar to response.redirect method that can be used to redirect to specific URL.
Example:
It is similar to server.transfer method that can be used to redirect to any controller action within the application.
Example:
Redirect using Hyperlink: MVC provides Html Helper "Html.ActionLink", that renders as "" tag to redirect to any location.
Syntax:
Example:
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
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:
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()RedirectToRouteResult
{
return Redirect("http://www.google.com");
}
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()* Add partial view to a view
{
return PartialView();
}
Public PartialViewResult Login()
{
return PartialView();
}
@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();
}