代码之家  ›  专栏  ›  技术社区  ›  NikolaiDante

在Asp.NETMVC中设置“主页”

  •  96
  • NikolaiDante  · 技术社区  · 17 年前

    在asp.net MVC中,“主页”(即点击www.foo.com时显示的路径)设置为Home/Index。

    • 这个值存储在哪里?
    • 如何更改“主页”?
    • 还有什么比在主控制器的索引操作中使用RedirectToRoute()更优雅的吗?

    我尝试在我的项目中对Home/Index进行grepping,但找不到引用,在IIS(6)中也看不到任何内容。我查看了根目录中的default.aspx页面,但似乎没有做任何相关的事情。

    谢谢

    7 回复  |  直到 13 年前
        1
  •  155
  •   ΩmegaMan    8 年前

    看这张照片 Default.aspx/Default.aspx.cs 以及Global.asax.cs

            routes.MapRoute(
                "Default", // Route name
                "",        // URL with parameters
                new { controller = "Home", action = "Index"}  // Parameter defaults
            );
    

    只需将控制器/操作名称更改为所需的默认名称。这应该是路由表中的最后一条路由。

        2
  •  25
  •   JTW    8 年前

    ASP.NET内核

    在中配置路由 Configure 方法 Startup

    app.UseMvc(routes =>
    {
       routes.MapRoute(
       name: "default",
       template: "{controller=FooController}/{action=Index}/{id?}");
    });
    

    ASP.NET前内核

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        // Here I have created a custom "Default" route that will route users to the "YourAction" method within the "FooController" controller.
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "FooController", action = "Index", id = UrlParameter.Optional }
        );
    }
    
        3
  •  5
  •   ΩmegaMan    8 年前

    步骤1:单击解决方案中的Global.asax文件。

    第2步:然后转到定义

    RouteConfig.RegisterRoutes(RouteTable.Routes);

    步骤3:更改控制器名称和视图名称

    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 = "Home", 
                                            action = "Index", 
                                            id = UrlParameter.Optional }
                            );
        }
    }
    
        4
  •  4
  •   benka Csaba    11 年前
    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 = "Your Controller", action = "Your Action", id = UrlParameter.Optional }
            );
        }
    }
    
        5
  •  3
  •   Michał Chaniewski    17 年前

    检查global.asax.cs中的RegisterRoutes方法-它是路由配置的默认位置。。。

        6
  •  3
  •   Community Mohan Dere    6 年前

    mvc5中的属性路由

    在MVC5之前,您可以通过调用 routes.MapRoute(...) 在RouteConfig.cs文件中。这是存储主页url的位置( Home/Index )。但是,如果按如下所示修改默认路线,

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

    请记住,这将影响其他操作和控制器的URL。例如,如果您有一个名为 ExampleController 其中的一个动作方法叫做 DoSomething ,然后是预期的默认url ExampleController/DoSomething

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

    现在 剂量 委员会的行动 示例控制器 类将映射到url hey/now . 但是,每当您想要为不同的操作定义路由时,这样做可能会变得单调乏味。因此,在MVC5中,您现在可以添加属性以将URL与这样的操作相匹配,

    public class HomeController : Controller
    {
        // url is now 'index/' instead of 'home/index'
        [Route("index")]
        public ActionResult Index()
        {
            return View();
        }
        // url is now 'create/new' instead of 'home/create'
        [Route("create/new")]
        public ActionResult Create()
        {
            return View();
        }
    }
    
        7
  •  1
  •   vickey1611    10 年前

    我试过这个答案,但对我不起作用。这就是我最后做的:

    return Redirect("~/Default.aspx")
    

    在RouteConfig.cs中,更改路线的controller=“Default”。

     routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
            );
    
        8
  •  0
  •   Martin Kei    7 年前

    如果您不想更改路由器,请转到HomeController 并在索引中的此处更改MyNewView,如下所示:

        public ActionResult Index()
        {
            return View("MyNewViewHere");
        }
    
    推荐文章