Server Utilities Migration

This sample demonstrates the ServerShim that provides Web Forms-compatible Server.MapPath(), Server.HtmlEncode(), Server.UrlEncode(), and ResolveUrl() in Blazor.


1. Server.MapPath()

In Web Forms, Server.MapPath("~/uploads") resolved a virtual path to a physical path. The BWFC shim provides the same API backed by IWebHostEnvironment.

Before (Web Forms)

string path = Server.MapPath("~/uploads");
// Returns: C:\inetpub\wwwroot\uploads

After (Blazor with BWFC)

// Same code works!
string path = Server.MapPath("~/uploads");
// Returns: /app/wwwroot/uploads
Live Demo - MapPath

Server.MapPath("~/images") resolves to:

/app/wwwroot/images


2. Server.HtmlEncode()

Encodes text for safe HTML display, preventing XSS. Same API as Web Forms.

Before (Web Forms)

string safe = Server.HtmlEncode(userInput);
lblOutput.Text = safe;

After (Blazor with BWFC)

// Same code works!
string safe = Server.HtmlEncode(userInput);
Live Demo - HtmlEncode

Encoded output: (enter text and click HtmlEncode)


3. Server.UrlEncode()

Encodes text for safe use in URLs. Same API as Web Forms.

Before (Web Forms)

string encoded = Server.UrlEncode(searchTerm);
Response.Redirect("search.aspx?q=" + encoded);

After (Blazor with BWFC)

// Same code works!
string encoded = Server.UrlEncode(searchTerm);
Live Demo - UrlEncode

Encoded output: (enter text and click UrlEncode)


4. ResolveUrl()

ResolveUrl("~/Products.aspx") strips the ~/ prefix and .aspx extension, producing a clean Blazor route.

Before (Web Forms)

string url = ResolveUrl("~/Products.aspx");
// Returns: /Products.aspx

After (Blazor with BWFC)

// Same call, cleaner output
string url = ResolveUrl("~/Products.aspx");
// Returns: /Products
Live Demo - ResolveUrl

ResolveUrl("~/Products.aspx") returns: /Products