0

Using Server.Transfer to show a page that informs the user that the web site is at maintenance mode.
At global.asax:

void Application_BeginRequest(object sender, EventArgs e)
{
    if (Request.IsLocal)
        return;

    if (ConfigurationManager.AppSettings["MaintenanceMode"] == "true")
    {
        if (Request.AcceptTypes != null && Request.AcceptTypes[0] == "text/html")
            Server.Transfer("~/UserMessage.aspx?Maintenance");
    }
}  

Works well except when looking at the page source code I see that the CSS path has been updated but images' paths are not.
Any suggestions?

toy4fun
  • 839
  • 2
  • 14
  • 33
  • 1
    Not a direct answer, but why don't you just drop an `app_offline.htm` in the web root, with whatever content you would like. This would get the behavior you're expecting (Unless `UserMessage.aspx` does something dynamic, which isn't available this way) – Matt Sieker May 18 '12 at 06:26
  • You mean image path inside css ? I hope not :) – Aristos May 18 '12 at 06:32
  • `UserMessage.aspx` shows all kinds of messages, including being at maintenance mode. Inside the page I'm using a single image, not inside a css. The css path is changed by the `Server.Transfer` command but the image's path does not. – toy4fun May 18 '12 at 06:36

2 Answers2

1

I would use app_offline.htm in the application root or at the very least Response.Redirect if I were you, Server.Transfer does not change the HTTP address, so you have to be careful redirecting all assets to the underlying page or make all addresses absolute

Kamyar Nazeri
  • 25,786
  • 15
  • 50
  • 87
  • When using `Response.Redirect` the page doesn't find css nor images. For this I've tried `Server.Transfer` instead. – toy4fun May 18 '12 at 06:44
  • How did you define your css/images, are you using relative path? I guess you need to make your resources absolute, at least for the login/Maintenance page – Kamyar Nazeri May 18 '12 at 06:46
  • The paths are all relative. By `absolute` you mean giving the whole `http://www.whatever.com` url? It will work but I'm trying to avoid this. – toy4fun May 18 '12 at 06:48
  • No I mean using exactly like above, this is an absolute path: "~/UserMessage.aspx?Maintenance" if you are using `Server.Transfer`, then the page url (which is not the correct one) is used for page assets (images/scripts,...) if they use relative path – Kamyar Nazeri May 18 '12 at 06:49
  • Doesn't work, the css path get updated correctly but the image path stays as-is (``) and isn't found. Maybe the solution would be to write the path from codebehind at page load. – toy4fun May 18 '12 at 06:52
  • thats right, unless you ' ` or set the img tag with `runat="server"` – Kamyar Nazeri May 18 '12 at 06:54
  • Tried all the combinations but adding the `runat="server"` solved it. That's odd, because the image itself reside inside a form that has this tag. Thanks for your answer. – toy4fun May 18 '12 at 06:58
  • The image might reside in the same folder as the form, however using `Server.Transfer` does not change the url, fooling the browser to look for an image in the wrong place – Kamyar Nazeri May 18 '12 at 07:00
  • This I know, but placing the image inside a form with a `runat="server"` tag doesn't mean that the image inherites this tag? – toy4fun May 18 '12 at 07:03
0

The server.transfer is actually stop the execution of a page and start execute a different page, the page that you give it.

The result is that the user see a different page under the same url.

This have nothing to do with image path. Also the images are not pass from asp.net except if you have set even images to pass from asp.net processing. Also on the code that you have give us you do not make any transfer if they are images, but you check for the text/html.

Now if you have move the execution to a different css, the image path is not change, only the page that the user see change.

Maybe you mean that you use the MapPath() and this is not change according to the new directory ?

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • I'm trying to override the login procedure and shows a maintenance mode message. The location is still at `Account` (where the login.aspx page reside) but I couldn't manage to change it so I've tried `Server.Transfer`. I'm only changing for `text/html` otherwise even the css isn't found. – toy4fun May 18 '12 at 06:42
  • think to use the app_offline.htm – Aristos May 18 '12 at 06:45
  • I'm not using `MapPath()` anywhere in my code. `UserMessage.aspx` shows all kinds of messages, including being at maintenance mode. Trying to be a bit generic... – toy4fun May 18 '12 at 06:45