After spending a very long time to build this site, i finally put it in production. The prime reason for the delay was hosting which i was not able to afford up until now.
Well everything seemed to work fine on my dev machine but once i put the code in production, my URL rewriting logic failed. I was using Gaidar Magdanurov's UrlRewiting solution. From what i observed the http module was not picking up on the application load. So as a last minute change i used UrlRewritingNet.UrlRewrite. I did not have to change much of the thing for this to work. Its pretty straight forward to setup and use.
First you have to add a config section
<configSections>
<section name="urlrewritingnet" requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter"/>
</configSections>
The next step is to define the http module ( This is where i think my other solution fails)
<httpModules>
<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter"/>
</httpModules>
Now you are ready to write the rules like so.
<urlrewritingnet rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString" defaultPage="default.aspx" defaultProvider="RegEx" xmlns="http://www.urlrewriting.net/schemas/config/2006/07">
<rewrites>
<add name="Post" virtualUrl="Post/(\d+)/(.*).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="ShowBlog.aspx?ID=$1" ignoreCase="true"/>
</rewrites>
</urlrewritingnet>
Well you would guess that this is all you do and everything works fine. Even i thought so, but when i was testing this, i logged into my site and the URL which i expected to be something like this "http://vinay.qsh.eu/Post/1/something.aspx" had become "http://vinay.qsh.eu/Post/1/something.aspx?ID=1". This broke my page because there are 2 parameters with the same id. It beats me as to why this is being added on login/logout. It is not a postback issue as every other postback is working fine.
How to fix this? This is what i have done.
Add a helper method to check whether if the URL is rewritten one or plain URL
1 public static bool URLRewritten(string LocalPath)
2 {
3 bool ruleFound = false;
4 foreach (UrlRewritingNet.Configuration.RewriteSettings rewriteSettings in UrlRewritingNet.Web.UrlRewriting.Configuration.Rewrites)
5 {
6 UrlRewritingNet.Web.RewriteRule rewrite = null;
7 string providerName = rewriteSettings.Provider;
8 if ((string.IsNullOrEmpty(providerName)))
9 {
10 rewrite = UrlRewritingNet.Web.UrlRewriting.CreateRewriteRule();
11 }
12 else
13 {
14 rewrite = UrlRewritingNet.Web.UrlRewriting.CreateRewriteRule(providerName);
15 }
16 rewrite.Initialize(rewriteSettings);
17
18
19 if ((rewrite.IsRewrite(LocalPath)))
20 {
21 ruleFound = true;
22 break;
23 }
24 }
25 return ruleFound;
26 }
Then use this function in the LoggedIn and LoggedOut events to manage the URL
1 protected void Login_LoggedIn(object sender, EventArgs e)
2 {
3 if (Helpers.URLRewritten(Request.Url.LocalPath))
4 {
5 if (Request.Url.PathAndQuery.Contains("ReturnUrl"))
6 Response.Redirect(Request.Params["ReturnUrl"].ToString());
7 else
8 Response.Redirect(Request.Url.LocalPath);
9 }
10 else
11 {
12 if (Request.Url.PathAndQuery.Contains("ReturnUrl"))
13 Response.Redirect(Request.Params["ReturnUrl"].ToString());
14 else
15 Response.Redirect(Request.Url.PathAndQuery);
16 }
17 }
18
19 protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
20 {
21 if (Helpers.URLRewritten(Request.Url.LocalPath))
22 {
23 Response.Redirect(Request.Url.LocalPath);
24 }
25 else
26 {
27 Response.Redirect(Request.Url.PathAndQuery);
28 }
29 }
One more thing. To manage the Return URL, you'll have to call the helper function like so
1 if (Helpers.URLRewritten(Request.Url.LocalPath))
2 {
3 Response.Redirect(FormsAuthentication.LoginUrl +
4 "?ReturnUrl=" + Request.Url.LocalPath);
5 }
6 else
7 {
8 Response.Redirect(FormsAuthentication.LoginUrl +
9 "?ReturnUrl=" + Request.Url.PathAndQuery);
10 }
I will be using this workaround until i find a fix.