28

When I build and run my application I get a directory listing in the browser (also happens for sub folders), and I have to click on Index.aspx. It's making me crazy.

Visual Studio 2008 ASP.NET Development Server 9.0.0.0

Dan Williams
  • 4,910
  • 11
  • 37
  • 46
  • 1
    This same question (asked after this one) also appears, with a different answer, [here](http://stackoverflow.com/questions/3398565/change-default-default-aspx-to-index-aspx-on-visual-studio-2010) – Jonathan Williams Aug 04 '11 at 16:32
  • Refer @Philippe Leybaert answer that says "Go to the project's properties page, select the "Web" tab " – LCJ Jun 07 '13 at 09:51

9 Answers9

39

Right click on the web page you want to use as the default page and choose "Set as Start Page" whenever you run the web application from Visual Studio, it will open the selected page.

James Conigliaro
  • 3,809
  • 19
  • 22
20

The built-in webserver is hardwired to use Default.aspx as the default page.

The project must have atleast an empty Default.aspx file to overcome the Directory Listing problem for Global.asax.

:)

Once you add that empty file all requests can be handled in one location.

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();
    }
}
zproxy
  • 3,509
  • 3
  • 39
  • 45
12

Go to the project's properties page, select the "Web" tab and on top (in the "Start Action" section), enter the page name in the "Specific Page" box. In your case index.aspx

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • 4
    Yup, that works for the start page, but when I browse to anything in a sub folder I get a folder listing again. – Dan Williams Jul 14 '09 at 13:16
  • It's not possible to specify a default page in Visual Studio's internal webserver (Cassini) – Philippe Leybaert Jul 14 '09 at 13:22
  • It's working as expected for several of the other developers in my group – Dan Williams Jul 14 '09 at 13:50
  • 2
    The built-in webserver is hardwired to use Default.aspx as the default page. Maybe your team members are using Default.aspx instead of index.aspx? Or maybe they're using the local IIS on their machine for development. – Philippe Leybaert Jul 14 '09 at 14:25
  • Thanks, now if I can only get it to start the web project all the time instead of the child projects when running the solution – StingyJack Jun 06 '11 at 12:10
9

Similar to zproxy's answer above I have used the folowing code in the Gloabal.asax.cs to achieve this:

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");
        }
    }
}
Jonathan Williams
  • 2,015
  • 1
  • 19
  • 23
1

One way to achieve this is to add a DefaultDocument settings in the Web.config.

  <system.webServer>
   <defaultDocument>
    <files>
      <clear />
      <add value="DefaultPage.aspx" />
    </files>
   </defaultDocument>
  </system.webServer>
Jahangir Alam
  • 353
  • 1
  • 4
  • 7
1
public class Global : System.Web.HttpApplication
{
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Request.Url.AbsolutePath.EndsWith("/"))
        {
             Server.Transfer("~/index.aspx");
        }
    }
}
Justin
  • 84,773
  • 49
  • 224
  • 367
Shabab
  • 11
  • 2
0

If you are running against IIS rather than the VS webdev server, ensure that Index.aspx is one of your default files and that directory browsing is turned off.

Garry Shutler
  • 32,260
  • 12
  • 84
  • 119
0

This One Method For Published Solution To Show SpeciFic Page on startup.

Here Is the Route Example to Redirect to Specific Page...

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

By Default Home Controllers Index method is executed when application is started, Here You Can Define yours.

Note : I am Using Visual Studio 2013 and "YourSolutionName" is to changed to your project Name..

Uncle Tech
  • 84
  • 5
0

I'm not sure what framework you are using but in ASP.NET MVC you can simply go to the App_Start folder and open the RouteConfig.cs file. The code should look something like this:

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

You can change the landing page on the last line of code there after defaults.

Cole Perry
  • 197
  • 21