Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
ActionResults in ASP.Net MVC
WhatsApp
Vithal Wadje
9y
25.7k
0
3
100
Article
Background
In many forum posts I have read one common question. That is, what are ActionResults in MVC. So this article explains ActionResults of MVC that shows the output to the client. So instead of going into depth on the subject, let us start with an overview.
ActionResult
ActionResult is the abstract class for showing the output to the client in various formats as in the result view returned by the controller. The ActionResult is defined in the controller and the controller returns to the client (the browser).
The following are the subtype or types of Action Results that are present in MVC that are used depending on the output requirement of the client.
ViewResult
PartialViewResult
EmptyResult
RedirectResult
RedirectToRouteResult
JsonResult
JavaScriptResult
ContentResult
FileContentResult
FileStreamResult
FilePathResult
The following is a brief explanation of each ActionResult.
ViewResult:
returns the view to the client as HTML.
The following is the syntax to define the ViewResult in a MVC controller.
Syntax
public
class
EmpController : Controller
{
public
ActionResult Index()
{
return
View();
}
}
PartialViewResult
: returns the partial view to the response stream, usually the partial view called from the main view. It's the same as a user control in ASP.Net.
Syntax
public
class
EmpController : Controller
{
public
ActionResult Index()
{
return
PartialView(
"_Empdetails"
);
}
}
EmptyResult:
this action returns the empty response to the client.
Syntax
public
class
EmpController : Controller
{
public
ActionResult Index()
{
return
new
EmptyResult();
}
}
RedirectResult:
redirects to the specified URL, the same as Response.Redirect in ASP.Net.
Syntax
public
class
EmpController : Controller
{
public
ActionResult Index()
{
return
Redirect(
"http://www.compilemode.com"
);
}
}
RedirectToRouteResult:
does an HTTP redirection to a URL that is determined by the routing engine, based on given route data.
Syntax
public
class
EmpController : Controller
{
public
ActionResult Index()
{
filterContext.Result =
new
RedirectToRouteResult(
new
RouteValueDictionary(
new
{
action =
"View"
,
controller =
"EmpController"
,}));
}
}
JsonResult
: serializes a given ViewData object into JavaScript Object Notation (JSON) format.
Syntax
public
class
EmpController : Controller
{
public
JsonResult EmpDet()
{
return
Json(
"HI"
, JsonRequestBehavior.AllowGet);
}
}
JavaScriptResult
: returns a piece of JavaScript code that can be executed on the client side.
Syntax
public
class
EmpController : Controller
{
public
virtual
JavaScriptResult EmpDet()
{
var script =
string
.Format(@"
MaxNotificaitonsToShow = {0};
ItemsPerPage = 5;",
GlobalSettings.MaxNotificaitonsToShow);
return
JavaScript(script);
}
}
ContentResult
: writes the content to the response stream without requiring a view.
Syntax
public
class
EmpController : Controller
{
public
ContentResult PersonInfo()
{
return
Content(
"Hi Vithal Wadje"
);
}
}
FileContentResult
: This action returns a file to the client.
Syntax
public
class
EmpController : Controller
{
public
FileResult DailyReport()
{
string
fileName = @
"c:\Vithal Wadje\DailyReport.pdf"
;
string
contentType =
"application/pdf"
;
return
new
FilePathResult(fileName, contentType);
}
}
FileStreamResult:
returns a file to the client that is provided by a Stream.
Syntax
public
class
EmpController : Controller
{
public
ActionResult DownloadFile()
{
string
Fileinfo =@
"c:\Vithal Wadje\DailyReport.txt"
;
byte
[] data = Encoding.UTF8.GetBytes(Fileinfo );
return
File(data,
"text/plain"
,
"DailyReport.txt"
);
}
}
FilePathResult:
returns a file result to the client.
Syntax
public
class
EmpController : Controller
{
public
FilePathResult DownloadFile(
string
file, Guid GuID)
{
return
File(FileStream,
"application/octet-stream"
, fileName);
}
}
Note
This article is only a brief description of ActionResults so beginners can understand. For more details please read my future article.
Summary
My next articles will go into depth about each ActionResult. I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.
ActionResults
ASP.NET MVC
ContentResult
Up Next
Ebook Download
View all
ASP.NET MVC 5: A Beginner’s Guide
Read by 28.1k people
Download Now!
Learn
View all
Membership not found