Setting the default page for ASP.NET (Visual Studio) server configuration

前端 未结 8 754
北荒
北荒 2020-12-05 13:38

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 makin

相关标签:
8条回答
  • 2020-12-05 14:06

    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

    0 讨论(0)
  • 2020-12-05 14:06

    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..

    0 讨论(0)
  • 2020-12-05 14:07
    public class Global : System.Web.HttpApplication
    {
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Request.Url.AbsolutePath.EndsWith("/"))
            {
                 Server.Transfer("~/index.aspx");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-05 14:10

    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>
    
    0 讨论(0)
  • 2020-12-05 14:19

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-05 14:19

    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");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题