URL Rewriting (for "fancy" URLs)

Aug 20, 2009 at 5:57 PM

I'm creating a social networking site using a combination of the book and the source code and I think both are brilliant. The issue I'm running into is that the "custom homepage" code discussed in the User Profiles section of the book works great with the Visual Studio web server but NOT with IIS (I have IIS 5 on my development machine and IIS 6 on my test server). Basically, it looks like the ASP.NET code is not even processing the URLs (e.g., http://localhost/Fisharoo/klewis). I get an "HTTP 404 - File Not Found" error instead of the nice PageNotFound.aspx page from the code.

I've done some research and it sounds like something along the lines of "wildcard mapping" in IIS may be what I need to force the ASP.NET application to process all URLs. However, I wanted to see what suggestions others may have for me.

Thanks!

Ken

Aug 20, 2009 at 10:09 PM
For IIS 6 wildcard mapping will fix it for sure. However, I am not sure if there is a fix for IIS 5!

Andrew Siemer
(661) 722-5960
(661) 600-2355
-----[ CHAT ]-----
MSN:asiemer@hotmail.com
YIM:andrewsiemer
GIM:andrewsiemer
AIM:WebOnTheBrain
-----[ PROFILES ]-----





From: [email removed]
To: [email removed]
Date: Thu, 20 Aug 2009 09:57:16 -0700
Subject: URL Rewriting (for "fancy" URLs) [community:66511]

From: kenlewisjr
I'm creating a social networking site using a combination of the book and the source code and I think both are brilliant. The issue I'm running into is that the "custom homepage" code discussed in the User Profiles section of the book works great with the Visual Studio web server but NOT with IIS (I have IIS 5 on my development machine and IIS 6 on my test server). Basically, it looks like the ASP.NET code is not even processing the URLs (e.g., http://localhost/Fisharoo/klewis). I get an "HTTP 404 - File Not Found" error instead of the nice PageNotFound.aspx page from the code.
I've done some research and it sounds like something along the lines of "wildcard mapping" in IIS may be what I need to force the ASP.NET application to process all URLs. However, I wanted to see what suggestions others may have for me.
Thanks!
Ken


Get back to school stuff for them and cashback for you. Try BingT now.
Aug 21, 2009 at 4:48 PM

Hi Andrew,

I added wildcard mapping and got the nicer PageNotFound.aspx response, which told me that the request was at least being handled by the URLRewrite code! I was then able to modify the code to handle the issues I was running into.

The great news is that now I have something that should work in both my development and eventual production environments.

Thanks for your response which encouraged me to stay on the wildcard mapping path.

Ken

 

Oct 14, 2009 at 2:10 PM

hi,kenlewisjr

can you tell me the details how to fix it?

thx a lot!

Oct 14, 2009 at 7:49 PM

I'd be happy to. Now, are you referring to the wildcard mapping or the URLRewrite code or both?

Ken

 

Oct 15, 2009 at 3:21 PM

both will be perfect!

thanks for you guide.

 

Nov 10, 2009 at 6:18 PM

Sorry it has taken me so long to respond - I was out of town and got swamped with work.

For wildcard mapping do a web search for your IIS version. I'm using IIS 5 for my dev machine and IIS 6 for the test server.

Now, for the URLRewrite.cs changes you'll want to debug your code while the web server is running. For that you need to use "Attach to Process..." (do a web search on this, too) from the debug menu in VS while your site is running against IIS. This will allow you to walk through the code and see why it's not behaving the way you want. I made my code changes based on my own code walk through.

I inserted the following starting at line 68 (after the foreach and before #region BLOGS):

            string path = application.Request.PhysicalPath;
            bool pathExists = System.IO.File.Exists(path);

            if (!pathExists) // see if it exists after adding Default.aspx (in the case of folder paths)
            {
                if (!path.ToLower().Contains("default.aspx")) // add default here since not added in IIS though fine with VS web server
                {
                    string page = "Default.aspx";

                    //if (!path.EndsWith("/")) // Add forward slash
                    //    page = "/" + page; ;

                    pathExists = System.IO.File.Exists(path + page);
                    if (pathExists)
                        context.Response.Redirect(application.Request.Path + page);
                }
            }

            if (!pathExists)
            {

Then, after the else in #region PROFILES (now line 206) I added:

                    // Get the path of the virtual directory which will be extracted
                    // to leave just the user name
                    string appPath = application.Request.ApplicationPath;

                    string username = application.Request.Path.Replace(appPath, "");
                    username = username.Replace("/", "");
                    

I hope this helps!

Ken