Set Default Page in Asp.net

2019-01-02 22:48发布

Is there any section or code which allows us to set default page in web.config ?

For example, when people first visit my website, I want them to see CreateThing.aspx rather than Default.aspx.

The solutions I know :

  1. Put this line of code => Response.Redirect("CreateThings.aspx") in Default.aspx Page_Load event but this method is really naive.

  2. We can use IIS (default page configuration) but I wanna do the same thing over Asp.NET application.

If you know a better solution, please let me know.

Thanks in advance.

P.S : The reason why I am asking about Web.Config is I believe Web.Config would help us through this.

Edit: I am just wondering how Microsoft .NET team forgot such a simple thing :)

Edit :

This could be another solution for now :

<defaultDocument>
    <files>
        <clear />
        <add value="Default.aspx" />
        <add value="Default.htm" />
        <add value="Default.asp" />
        <add value="index.htm" />
        <add value="index.html" />
        <add value="iisstart.htm" />
    </files>
</defaultDocument>

8条回答
SAY GOODBYE
2楼-- · 2019-01-02 22:58

If you are using forms authentication you could try the code below:

<authentication mode="Forms">
<forms name=".FORM" loginUrl="Login.aspx" defaultUrl="CreateThings.aspx" protection="All" timeout="30" path="/"> 
</forms>
</authentication>
查看更多
戒情不戒烟
3楼-- · 2019-01-02 23:09

If using IIS 7 or IIS 7.5 you can use

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

http://www.iis.net/ConfigReference/system.webServer/defaultDocument

查看更多
来,给爷笑一个
4楼-- · 2019-01-02 23:09

Tip #84: Did you know… How to set a Start page for your Web Site in Visual Web Developer?

Simply right click on the page you want to be the start page and say "set as start page".

As noted in the comment below by Adam Tuliper - MSFT, this only works for debugging, not deployment.

查看更多
对你真心纯属浪费
5楼-- · 2019-01-02 23:11

You can override the IIS default document setting using the web.config

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

Or using the IIS, refer the link for reference http://www.iis.net/configreference/system.webserver/defaultdocument

查看更多
地球回转人心会变
6楼-- · 2019-01-02 23:12

I had done all the above solutions but it did not work.

My default page wasn't an aspx page, it was an html page.

This article solved the problem. https://weblog.west-wind.com/posts/2013/aug/15/iis-default-documents-vs-aspnet-mvc-routes

Basically, in my \App_Start\RouteConfig.cs file, I had to add a line:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("");   // This was the line I had to add here!

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

Hope this helps someone, it took me a goodly while to find the answer.

查看更多
在下西门庆
7楼-- · 2019-01-02 23:15

Map default.aspx as HttpHandler route and redirect to CreateThings.aspx from within the HttpHandler.

<add verb="GET" path="default.aspx" type="RedirectHandler"/>

Make sure Default.aspx does not exists physically at your application root. If it exists physically the HttpHandler will not be given any chance to execute. Physical file overrides HttpHandler mapping.

Moreover you can re-use this for pages other than default.aspx.

<add verb="GET" path="index.aspx" type="RedirectHandler"/>

//RedirectHandler.cs in your App_Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for RedirectHandler
/// </summary>
public class RedirectHandler : IHttpHandler
{
    public RedirectHandler()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region IHttpHandler Members

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.Redirect("CreateThings.aspx");
        context.Response.End();
    }

    #endregion
}
查看更多
登录 后发表回答