asp.net mvc controllers
DESCRIPTION
TRANSCRIPT
![Page 1: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/1.jpg)
ASP.NET MVC
![Page 2: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/2.jpg)
Controllers
Controllers process incoming requests, handle user input and interactions, and execute appropriate application
logic. A controller class typically calls a separate view component to generate the HTML markup for the request
Controllers inherit from Controller class
User interactions are translated to action methods
All controllers must end with Controller suffix
Controller responsibilities
1. Locating the appropriate action method to call and validating that it can be called.
2. Getting the values to use as the action method's arguments.
3. Handling all errors that might occur during the execution of the action method.
4. Providing the default WebFormViewEngine class for rendering ASP.NET page types (views).
![Page 3: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/3.jpg)
Action methods
Action methods map to user interactions
Examples of user interactions include
• Entering a URL
• Submitting a form
• Clicking a link
![Page 4: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/4.jpg)
Action methods (Cont.)
The method must be public.
The method cannot be a static method.
The method cannot be an extension method.
The method cannot be a constructor, getter, or setter.
The method cannot have open generic types.
The method is not a method of the controller base class.
The method cannot contain ref or out parameters.
![Page 5: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/5.jpg)
Preventing a Public Method from Being Invoked
![Page 6: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/6.jpg)
Action methods Return Types
Most action methods return an instance of a class that derives from ActionResult
ViewResult Renders a view as a Web page.
PartialViewResult Renders a partial view, which defines a section of a view that can be rendered inside another view.
RedirectResult Redirects to another action method by using its URL.
RedirectToRouteResult Redirects to another action method.
ContentResult Returns a user-defined content type.
JsonResult Returns a serialized JSON object.
JavaScriptResult Returns a script that can be executed on the client.
FileResult Returns binary output to write to the response.
EmptyResult
![Page 7: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/7.jpg)
Action Methods Parameters
The values for action method parameters are retrieved from the request's data collection.
The data collection includes name/values pairs for form data, query string values, and cookie values.
The ASP.NET MVC framework can automatically map URL parameter values to parameter values for
action methods. By default, if an action method takes a parameter, the MVC framework examines
incoming request data and determines whether the request contains an HTTP request value with the
same name. If so, the request value is automatically passed to the action method
![Page 8: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/8.jpg)
Action Filters
An action filter is an attribute that you can apply to a controller action -- or an entire controller -- that
modifies the way in which the action is executed
Action Filters in the ASP.NET MVC framework
OutputCache – This action filter caches the output of a controller action for a specified amount
of time.
HandleError – This action filter handles errors raised when a controller action executes.
Authorize – This action filter enables you to restrict access to a particular user or role.
You can develop your custom filters.
![Page 9: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/9.jpg)
Action Filter Types
Authorization filters – Implements the IAuthorizationFilter attribute.
Action filters – Implements the IActionFilter attribute.
Result filters – Implements the IResultFilter attribute.
Exception filters – Implements the IExceptionFilter attribute.
![Page 10: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/10.jpg)
Implementing Custom Action Filters
Custom Action filters inherit from ActionFilterAttribute class and has the following methods
OnActionExecuting – This method is called before a controller action is executed.
OnActionExecuted – This method is called after a controller action is executed.
OnResultExecuting – This method is called before a controller action result is executed.
OnResultExecuted – This method is called after a controller action result is executed.
![Page 11: ASP.NET MVC controllers](https://reader031.vdocuments.site/reader031/viewer/2022013003/5491ecaeb47959a90c8b45c8/html5/thumbnails/11.jpg)
Action Selectors
ASP.NET MVC 3 defines a set of Action selectors which determine the selection of an Action. One of them is
ActionName, used for defining an alias for an Action. When we define an alias for an Action, the Action will
be invoked using only the alias; not with the Action name.
Selector Types
HttpGet
HttpPost