Asp.net mvc ASP MVC路由

Asp.net mvc ASP MVC路由,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,我试图通过执行以下操作将登录页面设置为默认页面,但由于某些原因失败。我的默认页面仍然设置为Home controller Index()页面。有人能告诉我还有哪些因素可能会影响路由行为吗 public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*

我试图通过执行以下操作将登录页面设置为默认页面,但由于某些原因失败。我的默认页面仍然设置为Home controller Index()页面。有人能告诉我还有哪些因素可能会影响路由行为吗

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

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


        }
    }
这是我的会话控制器:

public partial class SessionController : Controller
    {
        //
        // GET: /Session/
        public virtual ActionResult Login()
        {
            return View();
        }

        public virtual ActionResult Test()
        {
            return View();
        }

    }
这是my_layout.cshtml

<body>
    <header>
        <div class="content-wrapper">
            <div class="float-left">
                <p class="site-title">@Html.ActionLink("Net-Inspect Aero Dropbox", "Index", "Home")</p>
            </div>
            <div class="float-right">
                <nav>
                    <ul id="menu">
                        <li>@Html.ActionLink("Login", MVC.Session.Login())</li>
                        <li>@Html.ActionLink("Test", MVC.Session.Test())</li>
                        <li>@Html.ActionLink("Test2", "Login", "Session")</li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
    <div id="body">
        <section class="content-wrapper main-content clear-fix">
            @RenderBody()
        </section>
    </div>
    <footer>
        <div class="content-wrapper">
        </div>
    </footer>
</body>

@Html.ActionLink(“网检航空Dropbox”、“索引”、“主页”)

  • @ActionLink(“Login”,MVC.Session.Login())
  • @ActionLink(“Test”,MVC.Session.Test())
  • @ActionLink(“Test2”、“登录”、“会话”)
@RenderBody()
请注意,如果我将默认值更改为new{controller=“Home”、action=“Index”、id=UrlParameter.Optional},那么这三个Html操作链接都可以工作。但是,如果我按照我想要的方式设置它,即会话控制器Login()视图,那么第一个和第三个链接将无法工作。它似乎知道我将Login()视图设置为默认视图,因此当我单击链接时,页面不会更改,但Login()页面上的html不会显示。它仍然显示Home Index()视图中的内容

(顺便说一句,我使用的是T4MVC,但这应该会有所不同。)


谢谢你的帮助

您需要将登录页面重定向操作设置为主控制器上的索引方法。无需更改Global.asax上的默认路由。诸如此类:

    public ActionResult Index()
    {

        ViewBag.Message = "Welcome to ASP.NET MVC!";
        if (!User.Identity.IsAuthenticated)
        {
            return RedirectToAction("LogOn", "Users");
        }
        else
        {

        }
        return View();
    }

是的,这确实解决了问题。那么asp mvc框架会自动将默认页面设置为Home Index()?您的解决方案更像是一种“良好实践”还是一种“变通方法”,以避免更改下划线配置的麻烦。谢谢。它默认由MVC提供。asp MVC框架将默认页面设置为Home Index()。维护MVC的标准惯例是一个很好的做法。您可以在不破坏MVC模式的情况下获得MVC的每一项功能。因此,请尝试维护。谢谢:)