Creating a base controller default action for all ActionResults - asp.net-mvc

I'm trying to find out if its possible to create a controller or action result in MVC that will trigger for every action.
Basically I want a check to occur before people are directed to the url each time, if this check fails I will return a different view but I don't want to write this out for every single action in my controller.
Can this be done??

This can be done with a custom ActionFilterAttribute.
Overriding OnActionExecuting means I can put the same check in for all actions.

Related

rails admin, create a button that runs custom code

in my rails app I need to generate a bunch of buttons/links linked to several models to enable users to run some maintenance tasks (coded in the Backend).
I found the concept of actions in rails admin here: https://github.com/sferik/rails_admin/wiki/Actions, but I'm not quite clear how to use it, any ideas?
I thought I could add the buttons to the edit action for a model, but no ideas how to.
Sooo, rails uses an MVC paradigm. It sounds like you have the View and Model components under control, but you are missing your Controller logic. The Controller, is a place that handles any View logic using Model methods... ideally.
So, if you already have an existing controller that you are trying to add "Controller Actions" to... you need to define your custom method actions you want to create in your config/routes.rb file.
This will look like the following:
Create a route for your new controller action.
Add new controller action definition to your Controller.
Call controller action in your view's button link_to tag helper
Handle your custom logic in your new controller action using model methods
This is the cyclical MVC nature of Rails. M (Model), V (View), C (Controller).

What´s the difference between a custom action filter and a custom action selector in ASP.NET MVC?

I would like to know the differences between a custom action filter and a custom action selector in ASP.NET MVC.
Say we want to restrict who can have access to an action method on a controller based on some rules. I could either create an action filter extending the ActionFilterAttribute class or extending the ActionMethodSelectionAttribute class, so that I could have something like:
[MyRestriction]
public ActionResult AnyAction(){}
Could anyone explain the differences between them so that I can make the right decision?
If you look at the documentation for ActionMethodSelectionAttribute, you will see at the very bottom of the page that there are a number of other classes that derive from this attribute.
These include:
Microsoft.Web.Mvc.AjaxOnlyAttribute
System.Web.Mvc.AcceptVerbsAttribute
System.Web.Mvc.HttpDeleteAttribute
System.Web.Mvc.HttpGetAttribute
System.Web.Mvc.HttpHeadAttribute
System.Web.Mvc.HttpOptionsAttribute
System.Web.Mvc.HttpPatchAttribute
System.Web.Mvc.HttpPostAttribute
System.Web.Mvc.HttpPutAttribute
System.Web.Mvc.NonActionAttribute
In other words, these are the attributes which control which Action Method is selected during routing when there are several different choices to choose from (ie there are 2 different Index methods, one decorated with [HttpGet] and one with [HttpPost]).
ActionFilterAttribute, on the other hand, is called only when an action is actually executing.
Think about it this way, The selection can run even if the action doesn't execute, the ActionFilter only runs if it does. The selection filter is only used to determine whether the action is a match condition, the action filter is used to do some action before, after, etc.. the action or response is executed.

Call action from another Controller

I have a controller action.How can i call action method from another controller in asp.net mvc?
Is this a valid thing to do. Is it possible?
If you're first action is complete then you might redirect to another action.
If there is some common code between these two controllers that you don't want to duplicate then you'd probably look at creating a service which would have this and be called by both actions

How to test for a condition in every action in MVC without having to write it inline?

I need that every action in a controller checks for a specific condition.
If that condition is not met, the user must be redirected a specific action.
How do I do this without having to check the result of that condition in every action?
While writing this, it occourred to me that I could user an Attribute like the AuthorizeAttribute.
Do you have any thoughts on this? Is this a good idea?
You can create a custom class ActionFilterAttribute that overrides the OnActionExecuting method. You can use the Result property for the ActionFilterContext to have it redirect to the appropriate View.

asp.net mvc newbie question

I recently started to look into asp.net mvc. Here is my issue.
Say every page on an application needs a variable set by the user, e.g. a date. If the user starts from url I provide, it is all good as I ask for that date and save it for the session. How can I redirect the user to the first page if they save the some other url (to a different controller and action).
In other words, I guess I am looking for something like [Authorize] attribute but on an application level.
Thanks for any help.
I would probably create a base controller that all of my controllers derive from. In the base controller I'd override the OnActionExecuting method to check the session for the required variable. If the variable isn't present, I would set the ActionExecutingContext Result property to a RedirectToRouteResult to the appropriate controller/action to set the variable.
Another alternative is to create a custom FilterAttribute that you decorate the appropriate controllers/actions with that does basically the same thing. I would only do this if the filter was to apply only to certain controllers or actions and not all as you describe in your question.

Resources