Reputation: 3107
i have used url rewriting using custom modulerewriter in my asp.net application and it is working but the problem is that when i click on any button the default url in browser change to something like http://tipl.bizo.in/Download.aspx?busid=tipl which sholuld be replaced by http://tipl.bizo.in/tipl/Download so how can i achieve this on button click
my code for custom module rewriter is :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BizoClientTemplate.Config;
using System.Text.RegularExpressions;
using System.Data;
using Click_Expo.BusinessObjects;
using System.Web.Caching;
using System.Web.SessionState;
namespace BizoClientTemplate
{
public class ModuleReWriter : BaseModuleWriter
{
private string[] ArrPages = new string[] { "/Home", "/SendEnquiry", "/Aboutus", "/MediaGallery", "/Download", "/Products", "/Ourteam", "/Contactus", "/Error","/OurClients.aspx" };
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
{
// log information to the Trace object.
app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");
app.Context.Trace.Write("ModuleRewriter", "Requested Url: " + requestedPath);
string _stroriginalurl = app.Request.Url.AbsoluteUri.ToLower();
if (!string.IsNullOrEmpty(_stroriginalurl))
{
Regex regurltest = new Regex(@"^http(s)?://(www\.)?(.*)\.(bizo\.in)\/$", RegexOptions.IgnoreCase);
if (regurltest.IsMatch(_stroriginalurl))
{
string _getparam = regurltest.Match(_stroriginalurl).Groups[3].Value;
if (!_getparam.Trim().Equals("www"))
{
if (requestedPath.Trim() == "/")
{
requestedPath = string.Format("/{0}/home", _getparam);
}
else
{
if (!requestedPath.Contains(_getparam))
requestedPath = string.Format("/{0}{1}", _getparam, requestedPath);
}
}
}
}
//app.Context.Response.Write(requestedPath);
//app.Context.Response.Write("</br>");
//app.Context.Response.Write(app.Request.Url.AbsoluteUri);
//app.Context.Response.End();
// get the configuration rules
ReWriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
if (requestedPath.ToLower().Contains("css/") || requestedPath.ToLower().Contains("js/"))
return;
if (requestedPath.ToLower().Contains("images/") || requestedPath.ToLower().Contains("image/"))
return;
if (requestedPath.ToLower().Contains(".axd"))
return;
if (requestedPath.ToLower().Contains(".axpx"))
return;
if (requestedPath.ToLower().Contains(".ascx"))
return;
if (!IsExistsInPageList(requestedPath))
{
string _newrequestepath = CheckInDb_N_Replace(requestedPath);
if (!string.IsNullOrEmpty(_newrequestepath))
{
if (_newrequestepath.Contains("Page Not Found"))
{
if (!string.IsNullOrEmpty(_stroriginalurl))
{
if(_stroriginalurl.Contains("localhost"))
requestedPath = _newrequestepath;
Regex regurltest = new Regex(@"^http(s)?://(www\.)?(.*)\.(bizo\.in)\/$", RegexOptions.IgnoreCase);
if (regurltest.IsMatch(_stroriginalurl))
{
app.Context.Response.Redirect("http://bizo.in");
}
}
}
else
requestedPath = _newrequestepath;
}
}
// iterate through each rule...
for (int i = 0; i < rules.Count; i++)
{
// get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
// Create a regex (note that IgnoreCase is set...)
Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
// See if a match is found
if (re.IsMatch(requestedPath))
{
// match found - do any replacement needed
string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
// log rewriting information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
// Rewrite the URL
RewriterUtils.RewriteUrl(app.Context, sendToUrl);
break; // exit the for loop
}
}
// Log information to the Trace object
app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
}
private DataView GetWebSiteList()
{
DataView dvReturn;
try
{
DataTable dtWebsiteList = new DataTable();
BoMyBizoWebsiteMapCollection objBoMyBizoWebsiteMapCollection = new BoMyBizoWebsiteMapCollection(new Click_Expo.BoClickExpo());
dtWebsiteList = objBoMyBizoWebsiteMapCollection.GetMyBizoWebsiteMapTable("", "");
dvReturn = dtWebsiteList.DefaultView;
if (HttpContext.Current.Cache["SiteName"] == null)
{
DateTime dt = DateTime.Now.AddMinutes(30);
HttpContext.Current.Cache.Add("SiteName", dvReturn, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(dt.Ticks - DateTime.Now.Ticks), CacheItemPriority.High, null);
}
else
{
HttpContext.Current.Cache["SiteName"]=dvReturn;
}
}
catch (Exception ex)
{
throw new Exception("Modulerewriter:GetWebSiteList()" + ex.Message);
}
return dvReturn;
}
private DataView GetBusinesslist()
{
DataView dvReturn;
try
{
BoBusinessProfileCollection objBoBusinessProfileCollection = new BoBusinessProfileCollection(new Click_Expo.BoClickExpo());
DataTable dtBusinessSites = new DataTable();
dtBusinessSites = objBoBusinessProfileCollection.GetAllBusiness();
dvReturn = dtBusinessSites.DefaultView;
if (HttpContext.Current.Cache["Business"] == null)
{
DateTime dt = DateTime.Now.AddMinutes(30);
HttpContext.Current.Cache.Add("Business", dvReturn, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(dt.Ticks - DateTime.Now.Ticks), CacheItemPriority.High, null);
}
else
{
HttpContext.Current.Cache["Business"]= dvReturn;
}
}
catch(Exception ex)
{
throw new Exception("Module rewiter:GetBusinessWebsitelist()" + ex.Message);
}
return dvReturn;
}
private static bool isNumeric(string strToCheck)
{
bool Isstatus = false;
Regex rg = new Regex(@"^\d+$");
Isstatus = rg.IsMatch(strToCheck);
return Isstatus;
}
private string CheckInDb_N_Replace( string _key)
{
string _newpath="";
DataView dvSiteNames;
if (isNumeric(_key.Replace("/", "")))
{
dvSiteNames = GetWebSiteList();
if (dvSiteNames.Count == 0)
{
dvSiteNames = GetBusinesslist();
}
}
else
{
dvSiteNames = GetWebSiteList();
}
if (dvSiteNames.Count > 0)
{
if (isNumeric(_key.Replace("/", "")))
{
dvSiteNames.RowFilter = "BusinessId='" + _key.Replace("/", "") + "'";
if (dvSiteNames.Count == 0)
{
dvSiteNames = GetBusinesslist();
dvSiteNames.RowFilter = "BusinessId='" + _key.Replace("/", "") + "'";
if (dvSiteNames.Count > 0)
_newpath = string.Format("{0}/home", _key);
else
_newpath = string.Format("/FrmError/{0}", "Page Not Found");
}
else
{
_newpath = string.Format("{0}/home", _key);
}
}
else
{
dvSiteNames.RowFilter = "WebsiteName ='" + _key.Replace("/", "") + "'";
if (dvSiteNames.Count > 0)
_newpath = string.Format("{0}/home", _key);
else
_newpath = string.Format("/FrmError/{0}", "Page Not Found");
}
}
return _newpath;
}
private bool IsExistsInPageList( string _key)
{
bool isExists = false;
for (int i = 0; i < ArrPages.Length; i++)
{
if (_key.ToLower().Contains(ArrPages[i].ToLower()))
{
isExists = true;
break;
}
}
return isExists;
}
}
Upvotes: 3
Views: 2058
Reputation: 4585
Actually urlrewrite only rename the url of the request but asp.net actually does not know anything about this change. so you will have change something in the code to make you pages aware of the change. This will be a solution:
protected void Page_Load(object sender, EventArgs e)
{
form1.Action = Request.RawUrl;
}
for more information about the problem and solution check this out:
http://ruslany.net/2008/10/aspnet-postbacks-and-url-rewriting/
Regards.
Upvotes: 1