0

我想在 Asp.net mvc 中添加自定义默认页面,所以页面转到 Home/Index ,我想转到 Account/Login。我已经实现了以下,但它仍然去主页/索引。请告知我做错了什么。谢谢

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


        }
    }
4

3 回答 3

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 }
);
于 2013-11-12T05:48:34.717 回答
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..);            
        }
    }
于 2014-05-20T19:39:24.843 回答
0

用这个替换你的默认路由(最后一个)

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
            );
于 2013-11-12T03:09:35.770 回答