Complete Guide: Fixing IIS 404 Errors and Database Connection Issues for ASP.NET Websites
Complete Guide: Fixing IIS 404 Errors and Database Connection Issues for ASP.NET Websites
Introduction
When migrating an ASP.NET website from a live server to a local development environment, you may encounter various issues including 404 errors on internal pages, database connection failures, and stored procedure execution errors. This comprehensive guide walks through the step-by-step troubleshooting process to resolve these common issues.
Table of Contents
Understanding the Problem
Fix 1: URL Rewriting for Friendly URLs
Fix 2: Database Connection Issues
Fix 3: Stored Procedure Schema Issues
Fix 4: Character Encoding Problems
Prevention and Best Practices
Understanding the Problem
When testing a site locally with a custom domain binding (e.g., http://yourdomain.com), you might encounter:
404 errors when accessing friendly URLs like
/about-usDatabase connection failures
"Could not find stored procedure" errors even though the procedure exists
Garbled text with characters like
â€oeappearing
These issues typically stem from four main areas:
Missing URL rewrite rules in IIS
Incorrect database connection strings
SQL Server schema qualification problems
File encoding issues
Fix 1: URL Rewriting for Friendly URLs
The Problem
When accessing http://yourdomain.com/about-us, IIS returns a 404 error with:
Handler:
StaticFilePhysical Path:
C:\inetpub\wwwroot\httpdocs\about-us
This indicates IIS is treating the request as a physical folder/file rather than passing it to ASP.NET for processing.
The Solution
Step 1: Install URL Rewrite Module for IIS
Download and install the URL Rewrite Module from Microsoft:
Download: URL Rewrite Module for IIS
After installation, restart IIS:
iisreset
Step 2: Verify Installation
Open IIS Manager → Select your site → Look for URL Rewrite icon in the features view.
Step 3: Configure Rewrite Rules in web.config
Add the following rewrite rules to your web.config:
xml
Key Configuration Points:
Setting
Purpose
runAllManagedModulesForAllRequests="true"
Routes all requests through ASP.NET pipeline
stopProcessing="true"
Prevents further rule processing after a match
negate="true"
Skips rewrite if physical file/directory exists
Fix 2: Database Connection Issues
The Problem
Your application fails to connect to the database with errors like:
Could not find stored procedureLogin failed for user
Network-related errors
The Solution
Step 1: Verify Database Connectivity
Test the connection using SQLCMD:
cmd
Step 2: Update Connection Strings
Ensure your web.config has the correct database connection details:
xml
Step 3: Test the Database Connection
Create a diagnostic ASPX page:
aspx
Fix 3: Stored Procedure Schema Issues
The Problem
Your stored procedure exists but cannot be found:
text
Yet the procedure appears in sys.objects:
sql
Root Cause
The stored procedure exists in a non-default schema. When a stored procedure is called without a schema prefix, SQL Server looks in the default schema (typically dbo). If the procedure was created in a custom schema, it won't be found.
The Solution
Step 1: Find the Actual Schema
cmd
Example output:
text
Step 2: Apply One of These Fixes
Option A: Set Default Schema for User (Recommended)
cmd
Example:
cmd
Option B: Update Application Code
Change stored procedure calls to include the schema prefix:
csharp
Option C: Create a Wrapper Procedure
cmd
Step 3: Verify the Fix
cmd
Fix 4: Character Encoding Problems
The Problem
Text appears garbled with characters like â€oe, â€, or ‘ instead of proper quotes.
Root Cause
Content was copied from Microsoft Word which uses "smart quotes" (
“ ”and‘ ’)The file encoding is UTF-8 but the browser interprets it as ANSI/ISO-8859-1
The Solution
Option A: Set Correct Encoding in web.config
xml
Option B: Add Meta Charset to ASPX Pages
html
Option C: Fix the Text Content
Replace smart quotes with straight quotes:
Wrong Character
Correct Replacement
â€oe or “
"
†or ”
"
‘ or ‘
'
’ or ’
'
Example:
html
Option D: Save Files with UTF-8 Encoding
Using Notepad++:
Open the file
Click Encoding → Encode in UTF-8 without BOM
Save the file
Using VS Code:
Open the file
Click on the encoding indicator in the bottom bar
Select Save with Encoding → UTF-8
Prevention and Best Practices
1. URL Rewriting
Always use rewrite (not redirect) for friendly URLs to keep the URL clean
Test rewrite rules with
stopProcessing="true"to avoid conflictsKeep generic rules at the bottom of your rules list
2. Database Connections
Use Windows Authentication when possible for local development
Store connection strings securely; never commit production credentials to version control
Use separate connection strings for development and production:
xml
3. Database Schemas
Always use schema prefixes in stored procedure calls:
schema.procedure_nameSet appropriate default schemas for database users
Document custom schemas in your application documentation
4. Character Encoding
Always use UTF-8 without BOM for web files
Avoid copying directly from Microsoft Word; use Notepad as an intermediary
Set consistent encoding in your development environment
5. Diagnostic Tools
Create a diagnostic page to test your configuration:
aspx
Summary
Issue
Symptom
Solution
404 on friendly URLs
Handler: StaticFile
Install URL Rewrite module; add rewrite rules
Database connection
Login failed / timeout
Verify connection string; test with SQLCMD
Stored procedure not found
Procedure exists but can't execute
Set default schema or use schema prefix
Garbled text
â€oe characters appear
Set UTF-8 encoding; replace smart quotes
Quick Reference Commands
cmd
Conclusion
Migrating an ASP.NET application to a local development environment requires careful attention to URL rewriting, database connectivity, schema handling, and file encoding. By systematically addressing each of these areas, you can successfully set up a functional local testing environment that mirrors your production configuration.
Remember to:
Install required IIS modules (URL Rewrite)
Verify database connectivity before assuming schema issues
Check schema qualification when stored procedures can't be found
Maintain consistent file encoding across your project
Create diagnostic tools to quickly identify issues
With these fixes applied, your local site should function identically to the production environment, allowing for safe development and testing.
Last updated