简体   繁体   中英

Front Controller in MVC c#

Could someone tell me which is the front controller in MVC 4 c# visual studio please?

I mean, i have to do a big application and i want add security to restrict the access to the controllers and actions. I used to do this in the Logistic of the Front Controller in CodeIgniter, adding a token to the session, so if someone wanted to write the route manually on the browser he couldnt access.

I've been reading about [Authorize(Roles="Admin")] and i have to admit that is a solution, but that means i have to write in every method of the all controllers, and i want to have that centralized in the front-controller with IF/ELSE.

PD: If you don't know how to do this, at least try to tell me where can i find the front controller in MVC c# visual studio please.

Thanks for all.

There is no such thing as a front controller in ASP MVC. I think the thing you're looking for is some sort of base controller where all of the other controllers inherit from.

You can add this Authorize attribute to methods or classes (whole controllers). If every action needs this attribute I suggest to create a master controller and let every controller inherit from this controller.

There is no front controller in MVC. You need to create a base controller , And your every controller will inherit Base controller.

public class BaseController : Controller
    {

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var getControllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            var getActionName = filterContext.ActionDescriptor.ActionName;
            //Write your code here
        }
    }

Now Inherit your controller with Base controller.

public class AccountController : BaseController
    {

        //Your action goes here.
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM