14

My default document is in subfolder not in root how can i make it default in asp.net 2.0 website.

Tried iis7 default document setting to '/pages/default.aspx' '~/pages/default.aspx' but it didn't work.

mamu
  • 12,184
  • 19
  • 69
  • 92
  • If found that if you try to specify a default document like "subfolder/subfolder/file", the content will be found and served, but all the links are broken because the web browser still thinks it's at the original parent folder. The only way to do this properly is with an HTTP redirect, either through JavaScript or through a web server response. The best way to trigger a redirect response, IMO, is through a location tag in web.config that triggers a redirect only for your default document in the root, as opposed to turning on redirection for the folder as a whole. – Triynko May 08 '13 at 23:22
  • Just leave the default.aspx file blank in the root folder, and in your web.config file, make the redirection specific to that page with a "location" tag like this: `` – Triynko May 08 '13 at 23:24

5 Answers5

17

Default document is not the same as start page. Default document means if I requested mysite.com/somefolder and didn't specify a file, which file should IIS display.

If you want to use a specific page as your home page, create a Default.aspx file and write this in it's codebehind class:

public override void ProcessRequest(HttpContext context) {
    context.Response.Redirect("pages/default.aspx", true);
}

As the client might have disabled Javascript, a server side approach would be more reliable. However it's best to issue a permanent redirect instead of a simple Response.Redirect. Also doing it using JS will be bad from a SEO point of view.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • Doing it code-behind is an unnecessary drag on time - one line of JavaScript is all you need. – Greg Hurlman Jan 07 '09 at 20:11
  • If you want to avoid JS, an HTML meta-refresh is another easy HTML-only solution. – Greg Hurlman Jan 12 '09 at 01:45
  • This is the correct solution. The goal here is not to turn on http redirection for a folder all, but rather to ensure that if people visit that folder, they are redirected to a specific document in a subfolder. Since they didn't target a specific document, you must allow the default document to be invoked in the original folder (setting a default document like "subfolder/default.aspx" will serve the file but break the links, because the browser will still think it's in the parent folder), and then for that default document only, have it trigger a redirect. Aside from JavaScript, this is it. – Triynko May 08 '13 at 23:07
  • 4
    Better yet, you can do the exact same thing here without writing any code. Instead of putting that ProcessRequest handler in your default.aspx page, just leave default.aspx blank, and in your web.config file, make the redirection specific to that page with a "location" tag like this: `` – Triynko May 08 '13 at 23:18
9

You don't need to create a dummy Default.aspx page.

In your Global.asax.cs file, write the following:

public void Application_Start(object sender, EventArgs e)
{
    var routeCollection = RouteTable.Routes;
    routeCollection.MapPageRoute("DefaultRoute", string.Empty, "~/YourDesiredSubFolder/YourDesiredDocument.aspx");
}

Explanation:

  • Application_Start code is guaranteed to run once and only once on the application start.
  • The first line of code, gets a collection of the URL routes for your application.
  • The second line of code, defines a new route pointing to your inner page in the subfolder that you wish.
  • The second argument is empty to indicate that this route is used when there's no specific page is requested and there's no Default document existing.
Menahem
  • 3,974
  • 1
  • 28
  • 43
Mohamed Emad
  • 1,113
  • 1
  • 9
  • 16
  • In my Development Environment (VS2015CE) RouteTable.Routes is not a type but a property, So it is RouteTable.Routes .MapPageRoute("DefaultRoute", string.Empty, "~/YourDesiredSubFolder/YourDesiredDocument.aspx"); – Phoenix Sep 12 '16 at 10:05
2

Default documents are a subfolder-specific thing - what you're trying to do won't (directly) work. Set up a default.htm file in the root, and have it refresh to your real "home page".

The better question you should be asking is how on Earth your homepage got out of the root directory.

Greg Hurlman
  • 17,666
  • 6
  • 54
  • 86
0

In theory you could have a Web.config file inside the directory and use the defaultDocument element to set the default document. See here: https://stackoverflow.com/a/2012079/125938.
Unfortunately I haven't been able to get it to work myself locally, but that might be because it isn't supported in the Visual Studio development server.

Community
  • 1
  • 1
Protector one
  • 6,926
  • 5
  • 62
  • 86
0

Say "index.html" is the default page you want and it is present in "Public" subfolder.

Instead of specifying "/Public/index.html" as the default site, try "Public/index.html"