【问题标题】:Setting the default page for ASP.NET (Visual Studio) server configuration设置 ASP.NET (Visual Studio) 服务器配置的默认页面
【发布时间】:2010-11-10 15:35:36
【问题描述】:

当我构建和运行我的应用程序时,我会在浏览器中看到一个目录列表(子文件夹也会出现),我必须单击 Index.aspx。这让我发疯了。

Visual Studio 2008 ASP.NET 开发服务器 9.0.0.0

【问题讨论】:

  • 同样的问题(在这个问题之后提出)也出现了,但答案不同,here
  • 参考@Philippe Leybaert 回答“转到项目的属性页面,选择“Web”选项卡“

标签: asp.net visual-studio-2008 configuration


【解决方案1】:

我不确定您使用的是什么框架,但在 ASP.NET MVC 中,您可以简单地转到 App_Start 文件夹并打开 RouteConfig.cs 文件。代码应如下所示:

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

您可以在defaults之后的最后一行代码中更改登录页面。

【讨论】:

    【解决方案2】:

    实现此目的的一种方法是在 Web.config 中添加 DefaultDocument 设置。

      <system.webServer>
       <defaultDocument>
        <files>
          <clear />
          <add value="DefaultPage.aspx" />
        </files>
       </defaultDocument>
      </system.webServer>
    

    【讨论】:

      【解决方案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 },
                  namespaces: new[] { "YourSolutionName.Controllers" }
              );
          }
      }
      

      默认情况下 Home Controllers Index 方法在应用程序启动时执行,您可以在此处定义。

      注意:我正在使用 Visual Studio 2013,“YourSolutionName”将更改为您的项目名称..

      【讨论】:

        【解决方案4】:
        public class Global : System.Web.HttpApplication
        {
            protected void Application_BeginRequest(object sender, EventArgs e)
            {
                if (Request.Url.AbsolutePath.EndsWith("/"))
                {
                     Server.Transfer("~/index.aspx");
                }
            }
        }
        

        【讨论】:

        • 这个答案不太对。如果用户访问“site.com/account/”,用户将被重定向到“/index.aspx”,而不是“/account/index.aspx”
        • “模仿是最真诚的奉承。” - 查尔斯·卡莱布·科尔顿
        【解决方案5】:

        与上面 zproxy 的回答类似,我使用 Gloabal.asax.cs 中的以下代码来实现这一点:

        public class Global : System.Web.HttpApplication
        {
            protected void Application_BeginRequest(object sender, EventArgs e)
            {
                if (Request.Url.AbsolutePath.EndsWith("/"))
                {
                    Server.Transfer(Request.Url.AbsolutePath + "index.aspx");
                }
            }
        }
        

        【讨论】:

          【解决方案6】:

          内置的网络服务器被硬连线使用 Default.aspx 作为默认页面。

          该项目必须至少有一个空的Default.aspx 文件才能解决Global.asax 的目录列表问题。

          :)

          添加该空文件后,所有请求都可以在一个位置处理。

          public class Global : System.Web.HttpApplication
          {
              protected void Application_BeginRequest(object sender, EventArgs e)
              {
                  this.Response.Write("hi@ " + this.Request.Path + "?" + this.Request.QueryString);
                  this.Response.StatusCode = 200;
                  this.Response.ContentType = "text/plain";
          
                  this.Response.End();
              }
          }
          

          【讨论】:

            【解决方案7】:

            如果您针对 IIS 而不是 VS webdev 服务器运行,请确保 Index.aspx 是您的默认文件之一,并且目录浏览已关闭。

            【讨论】:

            • 不,我正在运行 VS webdev 服务器。
            【解决方案8】:

            右键单击要用作默认页面的网页,然后在从 Visual Studio 运行 Web 应用程序时选择“设置为起始页”,它将打开所选页面。

            【讨论】:

            • 是的,这适用于起始页,但是当我浏览到子文件夹中的任何内容时,我会再次获得一个文件夹列表。
            【解决方案9】:

            转到项目的属性页面,选择“Web”选项卡,然后在顶部(在“开始操作”部分),在“特定页面”框中输入页面名称。在您的情况下 index.aspx

            【讨论】:

            • 是的,这适用于起始页,但是当我浏览到子文件夹中的任何内容时,我会再次获得一个文件夹列表。
            • 无法在 Visual Studio 的内部网络服务器 (Cassini) 中指定默认页面
            • 我小组中的其他几个开发人员都按预期工作
            • 内置网络服务器硬连线使用 Default.aspx 作为默认页面。也许您的团队成员正在使用 Default.aspx 而不是 index.aspx?或者他们可能正在使用他们机器上的本地 IIS 进行开发。
            • 谢谢,现在如果我只能让它在运行解决方案时始终启动 Web 项目而不是子项目
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-02
            • 2010-12-27
            • 1970-01-01
            相关资源
            最近更新 更多