views:

265

answers:

3

Hi everyone,

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 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>
+3  A: 

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
}
this. __curious_geek
So, you say when ever a request happens to `Default.aspx`, the handler will redirect the request to `CreateThing.aspx` . It looks a generic solution. Thank you.
Braveyard
But would it cause HttpHandler pollution ?
Braveyard
After your edit, I need to say : Well it could be. I think the simple thing would be like `Application.Run(new Form()1)` :)
Braveyard
@Arron: You can always create a custom configuration section that will configure your `HttpHandler` for various different requests. You can also catch all *.aspx requests and see if request matches any of your configured URLs. Otherwise just pass it through.
Robert Koritnik
+1  A: 

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>
Zooking
To use Form Authentication, should I use the providers MemberShip or stuff ? I mean when I simply select Authentication Mode as Form rather than Windows, this code will work charmingly right ?
Braveyard
Everything works charmingly if you put charm in it. ;)
Robert Koritnik
I would say that this depends on the solution. If you need a more complex solution with different user profiles then you should go with MembershipProviders. But if it is a more simple setup you could just use <allow users=""/> and <deny users=""/>.
Zooking
+5  A: 

If your using IIS 7 you can use

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

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

David G
I think this is he best solution. It looks generic and easy to tell IIS what is going on. Thank you.
Braveyard