1
Server.Transfer("~\\Error Pages\\404.aspx");

Since ~ returns a path with backslashes

Or:

Server.Transfer("~/Error Pages/404.aspx");

Seen used and fits the docs ("The URL path") more

I tried and they both work.

So why am I asking?

Because if you don't do something the correct way, it might work now, but it might fail at some point and then it can be pretty difficult to debug.

ispiro
  • 26,556
  • 38
  • 136
  • 291
  • Did you flicked through these https://stackoverflow.com/q/38428561/4731319 https://stackoverflow.com/q/1589930/4731319? – gpro Apr 17 '19 at 20:26
  • @gpro I know the difference. However the question is whether I should use paths or url paths. But thanks. I'm now editing the question. – ispiro Apr 17 '19 at 20:32

2 Answers2

1

The right way to use is

Server.Transfer("~/Error Pages/404.aspx");

Example 1

Example 2

josejamesp
  • 52
  • 3
  • Worth an upvote, so +1. But still, the first is just a 3rd party which might be wrong. As for the second. It's outdated. The [current docs](https://learn.microsoft.com/en-us/dotnet/api/system.web.httpserverutility.transfer?view=netframework-4.7.2) don't contain an example. I'm not even sure that your link is for the exact same thing. But as I said. +1 nevertheless. Thanks. – ispiro Apr 17 '19 at 20:05
1

Both work, but using / is prefered, as \ internally gets replace by /.

From the .NET Reference Source:

HttpServerUtility.Transfer calls its Execute method which uses
VirtualPath virtualPath = VirtualPath.Create(path);

This VirtualPath.Create calls UrlPath.FixVirtualPathSlashes(virtualPath) which replaces \ with /.

internal static String FixVirtualPathSlashes(string virtualPath) 
{ 
    // Make sure we don't have any back slashes
    virtualPath = virtualPath.Replace('\\', '/');

    ...
}
pfx
  • 20,323
  • 43
  • 37
  • 57