dalegroupco
09-28-2009, 10:34 AM
Hello all,
I am using the following code as a simple to test URL rewriting in the production environment. This code works perfectly on my dev and test machines, though it does not work at all on the DASP server, and instead the request is redirected to the custom404 page.
in Application_BeginRequest, I call the rewriter as:
UrlRewriter.xmsUrlRule.WriteRuleFor(HttpContext.Cu rrent.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length - 1]);
this is the code of the called method:
namespace UrlRewriter
{
public class xmsUrlRule
{
public string url { set; get; }
public string path { set; get; }
public string condition { set; get; }
public string ignore { set; get; }
public static void WriteRuleFor(string url)
{
xmsUrlRule rule = (from r in XElement.Load(global.GetPath("pathToFile.xml")).Elements("condition").Elements("rule")
where r.Attribute("url").Value == url
select new xmsUrlRule
{
ignore = r.Parent.Attribute("ignore").Value,
condition = r.Parent.Attribute("check").Value,
path = r.Attribute("path").Value
}).First();
if (rule != null)
{
bool reWrite = true;
foreach (string ignoreDir in rule.ignore.Split(','))
{
if (ignoreDir.Contains(url))
reWrite = false;
else
continue;
}
if (reWrite)
{
//HttpContext.Current.RewritePath(rule.path, true);
//HttpContext.Current.Server.TransferRequest(rule.pa th, true);
HttpContext.Current.Server.Transfer(rule.path, true);
//HttpContext.Current.Response.Redirect(rule.path, true);
//I have tried all 4 of the above methods, none of which have worked
}
}
}
}
}
and here is an example line from the xml file:
<rule url="insurance" path="commercial-insurance.aspx" />
so basically any request for http://www.mysite.com/insurance should redirect the request to http://www.mysite.com/commercial-insurance.aspx but instead it is being caught by the custom404.aspx page. To solve this problem I searched for solutions and thought that I could add the call to the rewrite method again in the custom404.aspx pages OnInit event:
UrlRewriter.xmsUrlRule.WriteRuleFor(HttpContext.Cu rrent.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length - 1]);
But to no avail, the request still ends on the 404 page. Does anyone have any suggestion as to what I am doing wrong? I really am unsure what I can do to fix this issue.
Thanks,
Shawn
I am using the following code as a simple to test URL rewriting in the production environment. This code works perfectly on my dev and test machines, though it does not work at all on the DASP server, and instead the request is redirected to the custom404 page.
in Application_BeginRequest, I call the rewriter as:
UrlRewriter.xmsUrlRule.WriteRuleFor(HttpContext.Cu rrent.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length - 1]);
this is the code of the called method:
namespace UrlRewriter
{
public class xmsUrlRule
{
public string url { set; get; }
public string path { set; get; }
public string condition { set; get; }
public string ignore { set; get; }
public static void WriteRuleFor(string url)
{
xmsUrlRule rule = (from r in XElement.Load(global.GetPath("pathToFile.xml")).Elements("condition").Elements("rule")
where r.Attribute("url").Value == url
select new xmsUrlRule
{
ignore = r.Parent.Attribute("ignore").Value,
condition = r.Parent.Attribute("check").Value,
path = r.Attribute("path").Value
}).First();
if (rule != null)
{
bool reWrite = true;
foreach (string ignoreDir in rule.ignore.Split(','))
{
if (ignoreDir.Contains(url))
reWrite = false;
else
continue;
}
if (reWrite)
{
//HttpContext.Current.RewritePath(rule.path, true);
//HttpContext.Current.Server.TransferRequest(rule.pa th, true);
HttpContext.Current.Server.Transfer(rule.path, true);
//HttpContext.Current.Response.Redirect(rule.path, true);
//I have tried all 4 of the above methods, none of which have worked
}
}
}
}
}
and here is an example line from the xml file:
<rule url="insurance" path="commercial-insurance.aspx" />
so basically any request for http://www.mysite.com/insurance should redirect the request to http://www.mysite.com/commercial-insurance.aspx but instead it is being caught by the custom404.aspx page. To solve this problem I searched for solutions and thought that I could add the call to the rewrite method again in the custom404.aspx pages OnInit event:
UrlRewriter.xmsUrlRule.WriteRuleFor(HttpContext.Cu rrent.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length - 1]);
But to no avail, the request still ends on the 404 page. Does anyone have any suggestion as to what I am doing wrong? I really am unsure what I can do to fix this issue.
Thanks,
Shawn