ASP.Net MVC controller
- MVC controller use user request in browser.
- These are liable for responding to request made towards ASP.Net MVC web site.
- Browser request is draw to a Controller.
How to add Controller in MVC
Let's start by making a controller class.Solution Explorer, right-click the Controllers folder and then choose Add Controller.
Write controller Name "HelloWorldController". Left the default template as Empty MVC controller and click Add.
Observe thatSolution Explorer that a new file has been generated named HelloWorldController.cs. The file is open in the IDE.
Describing with example
When running a MVC default website, the browser request get mapped in below manner.
Url in browser is
So mapping would be like below
Home -> Controller class
About -> Action ( Method inside HomeController class)
- These are the public method inner a Controller class. Through these methods user communicate to ASP.Net MVC web site. These methods are display to outside world.
- Any Action could be bring from outside just by writing its address in address bar of browser.
- Action return type should be ActionResult. It might have any different return type also like integer or string . But if it is not ActionResult, return data will get exchange into string and then get provide.
These are the output of returned by the Actions. Action result is output which controller returns in response of browser request.
These are types of action result assist by MVC Controller.
All types of action output is inherited from ActionResult base class.
|ViewResult||Denoted HTML and markup|
|EmptyResult||Denoted NO Result|
|RedirectResult||Denoted a redirection to a new URL|
|ContentResult||Denoted a text result|
|FileContentResult||Denoted a downloadable file (with the binary content).|
|FilePathResult||Denoted a downloadable file (with a path).|
|FileStreamResut||Denoted a downloadable file (with a file stream).|
Important: action result are not get called directly. Alternatively of them method of controller base class get called.
List of methods of Controller base class is as follows
- View - Returns a ViewResult action result.
- Redirect - Returns a RedirectResult action result.
- RedirectToAction - Returns a RedirectToRouteResult action result.
- RedirectToRoute - Returns a RedirectToRouteResult action result.
- Json - Returns a JsonResult action result.
- Content - Returns a ContentResult action result.
- File - Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.