在asp.net mvc中创建自定义默认页面

时间:2013-11-12 03:02:16

标签: asp.net-mvc-4

我想在Asp.net mvc中添加自定义默认页面,所以相反页面转到Home / Index,我想转到帐户/登录。我已经实现了以下内容,但它仍然是Home / Index。请告诉我我做错了什么。谢谢

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 个答案:

答案 0 :(得分:3)

更改路线。默认路由设置为/ Home / Index

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

您可以将其更改为您希望的任何路线

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

答案 1 :(得分:3)

假设您希望将用户重定向到登录页面或自定义页面,如果他们没有“登录”。

您可以创建过滤器属性。

示例:

    [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..);            
        }
    }

答案 2 :(得分:0)

用这个

替换你的默认路线(最后一个)
routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
            );