Action selectors are attributes that can be applied to an action method in a controller. When we are creating action methods in the controller class by default they will look for respective view i.e. let say we have index action method when we request index action method in URL it will look for Index view in the view folder. But by using Action selector we can redirect the URL request to different View.
For Example:public class HomeController : Controller
{
public string Index()
{
return "Index action method invoked";
}
}
The above will respond to /Home/Index URL but If you want to invoke Index() action method, with the following URL /Home/List, you need to decorate the action method with ActionName attribute as shown below.
public class HomeController : Controller
{
[ActionName("List")]
public string Index()
{
return "Index action method invoked";
}
}
Now, if you navigate to /Home/Index, you will get an error - The resource cannot be found As it is looking for a view named List in the Home controller.
If you want to respond to the /Home/Index then you can explicitly tell the controller action method which view need to be responded as below.
[ActionName("List")]
public ActionResult Index()
{
return View("Index");
}
In the above code even if it is decorated with [ActionName("List")] it will respond to the Index View as we have mentioned return type as Index View.
Action Filters: Action filters are attributes that can be applied either on a controller action method or on a controller. When applied at the controller level, they are applicable for all actions within that controller. Action filters allow us to add, pre and post processing logic to an action method. This means they allow us to modify the way in which an action is executed.
Below are the name of some Action Filters:
Authorize
ChildActionOnly
HandleError
OutputCache
RequireHttps
ValidateInput
ValidateAntiForgeryToken
ActionResult: ActionResult is used with controller action method as return type. It is an abstract class and has several subtypes. An action method in a controller can return a wide range of objects. For example, an action method can return
1. ViewResult
2. PartialViewResult
3. JsonResult
4. RedirectResult etc..
For Example:public class HomeController : Controller
{
public string Index()
{
return "Index action method invoked";
}
}
The above will respond to /Home/Index URL but If you want to invoke Index() action method, with the following URL /Home/List, you need to decorate the action method with ActionName attribute as shown below.
public class HomeController : Controller
{
[ActionName("List")]
public string Index()
{
return "Index action method invoked";
}
}
Now, if you navigate to /Home/Index, you will get an error - The resource cannot be found As it is looking for a view named List in the Home controller.
If you want to respond to the /Home/Index then you can explicitly tell the controller action method which view need to be responded as below.
[ActionName("List")]
public ActionResult Index()
{
return View("Index");
}
In the above code even if it is decorated with [ActionName("List")] it will respond to the Index View as we have mentioned return type as Index View.
Action Filters: Action filters are attributes that can be applied either on a controller action method or on a controller. When applied at the controller level, they are applicable for all actions within that controller. Action filters allow us to add, pre and post processing logic to an action method. This means they allow us to modify the way in which an action is executed.
Below are the name of some Action Filters:
Authorize
ChildActionOnly
HandleError
OutputCache
RequireHttps
ValidateInput
ValidateAntiForgeryToken
ActionResult: ActionResult is used with controller action method as return type. It is an abstract class and has several subtypes. An action method in a controller can return a wide range of objects. For example, an action method can return
1. ViewResult
2. PartialViewResult
3. JsonResult
4. RedirectResult etc..
It's a good practice to return specific sub-types, but, if different paths of the action method return different subtypes, then It would better to return an ActionResult object as it is base for all. Below are the Action Result Sub Types
- HttpNotFoundResult: Returns an object to indicate that the requested resource cannot be found
- HttpUnauthorizedResult: Represents the result of an unauthorized HTTP request
- JavaScriptResult: Returns a piece of JavaScript code that can be executed on the client
- FileContentResult: Returns a file to the client
- FilePathResult: Returns a file to the client, which is provided by the given path
- FileStreamResult: Returns a file to the client, which is provided by a stream
- PartialViewResult: Returns a specified partial view
- ViewResult: Returns a specified view
- ContentResult: Writes content to the response stream without requiring a view
- JsonResult: Returns a JsonResult which serializes an object in JSON format
- EmptyResult: An empty response is returned. Used when the action method returns void.
- RedirectResult: Performs an HTTP redirection to a specified ne URL
- RedirectToRouteResult: Performs an HTTP redirection to another action method that is determined by the routing engine, based on given route data
No comments:
Post a Comment