Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The cleanest way in my opinion would be to create a custom route handler to be used by your default route. Then you can separate out which controller to be used if the controller name is your default controller name, in the example below, it is: Home. Then check if the user is an administrator or not and process the request with the controller you would like to use.</p> <p>Here is the code:</p> <pre><code>public class CustomHttpHandler : IHttpHandler { public RequestContext RequestContext { get; private set; } public CustomHttpHandler(RequestContext requestContext) { try { string controllerName = RequestContext.RouteData.GetRequiredString("controller"); if (controllerName.Equals("home", StringComparison.CurrentCultureIgnoreCase)) { bool isAdmin = RequestContext.HttpContext.User.IsInRole("Admin"); controllerName = isAdmin ? "admin" : "normaluser"; } IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory(); IController controller = factory.CreateController(RequestContext, controllerName); if (controller != null) { controller.Execute(RequestContext); } } finally { factory.ReleaseController(controller); } } } public class CustomRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new CustomHttpHandler(requestContext); } } // Now use the CustomRouteHandler when you map your default route. routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } ).RouteHandler = new CustomRouteHandler(); </code></pre> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload