0

I would like to add custom default page in Asp.net mvc so instead the page going to Home/Index , i would like to go to Account/Login. I have implement the following but it still go to Home/Index. Please advise what i did wrong. Thank you

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
            name: "Custom",
            url: "{controller}/{Account}/{page}",
            defaults: new
            {
                category = UrlParameter.Optional,
                page = 1,
                action = "Login"
            },
                constraints: new
                {
                    controller = "Account"
                }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );


        }
    }

3 Answers 3

3

Assuming the case that you want the users to be redirected to the login page or a custom page if they are not 'signed-in'.

You could create a filter attribute.

example:

    [RequireHttps]
    [AuthorizationFilter]
    public class MyController : Controller
    {
    }


    public class AuthorizationFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
        //do some validation, find if the user is signed-in.

            filterContext.Result = new RedirectResult(..Some where in the site..);            
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

3

Change your route. The default route is set to /Home/Index

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional }
);

You can change that to be any route you wish

routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters*
        new { controller = "ControllerName", action = "ControllerActionName", 
        id = UrlParameter.Optional }
);

Comments

0

replace your default route (the last one) with this

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
            );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.