代码之家  ›  专栏  ›  技术社区  ›  Dan Williams

设置ASP.NET(Visual Studio)服务器配置的默认页面

  •  27
  • Dan Williams  · 技术社区  · 15 年前

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

    Visual Studio 2008

    7 回复  |  直到 15 年前
        1
  •  39
  •   James Conigliaro    15 年前

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

        2
  •  20
  •   zproxy    15 年前

    内置Web服务器通过硬连线使用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();
        }
    }
    
        3
  •  11
  •   Philippe Leybaert    15 年前

    转到项目的属性页,选择“Web”选项卡,并在顶部(在“开始操作”部分)的“特定页面”框中输入页面名称。就你而言

        4
  •  8
  •   Jonathan Williams    13 年前

    与上面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");
            }
        }
    }
    
        5
  •  1
  •   Justin    13 年前
    public class Global : System.Web.HttpApplication
    {
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Request.Url.AbsolutePath.EndsWith("/"))
            {
                 Server.Transfer("~/index.aspx");
            }
        }
    }
    
        6
  •  1
  •   Jahangir Alam    4 年前

      <system.webServer>
       <defaultDocument>
        <files>
          <clear />
          <add value="DefaultPage.aspx" />
        </files>
       </defaultDocument>
      </system.webServer>
    
        7
  •  0
  •   Garry Shutler    15 年前

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

        8
  •  0
  •   Amneu    7 年前

    这是发布的解决方案在启动时显示特定页面的一种方法。

    下面是重定向到特定页面的路由示例。。。

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

    默认情况下,主控制器索引方法在应用程序启动时执行,您可以在这里定义自己的索引方法。

        9
  •  0
  •   Cole Perry    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 }
                );
            }
        }
    

    defaults .