C# asp.net中单击按钮时的url重写问题
我在我的asp.net应用程序中使用了使用自定义ModulerWriter的url重写,它正在工作,但问题是,当我单击任何按钮时,浏览器中的默认url将更改为类似于将被替换的url,因此如何在单击按钮时实现这一点 我的自定义模块重写器代码为:C# asp.net中单击按钮时的url重写问题,c#,asp.net,C#,Asp.net,我在我的asp.net应用程序中使用了使用自定义ModulerWriter的url重写,它正在工作,但问题是,当我单击任何按钮时,浏览器中的默认url将更改为类似于将被替换的url,因此如何在单击按钮时实现这一点 我的自定义模块重写器代码为: using System; using System.Collections.Generic; using System.Linq; using System.Web; using BizoClientTemplate.Config; using Syst
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;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用BizoClientTemplate.Config;
使用System.Text.RegularExpressions;
使用系统数据;
使用Click_Expo.BusinessObjects;
使用System.Web.Caching;
使用System.Web.SessionState;
命名空间BizoClientTemplate
{
公共类ModuleWriter:BaseModuleWriter
{
private string[]ArrPages=new string[]{/Home'、“/SendEnquiry”、“/Aboutus”、“/MediaGallery”、“/Download”、“/Products”、“/Ourteam”、“/Contactus”、“/Error”、“/OurClients.aspx”};
受保护的覆盖无效重写(字符串requestedPath,System.Web.HttpApplication应用程序)
{
//将信息记录到跟踪对象。
app.Context.Trace.Write(“ModuleRewriter”,“输入ModuleRewriter”);
app.Context.Trace.Write(“ModuleWriter”,“请求的Url:+requestedPath”);
string _stroriginalurl=app.Request.Url.AbsoluteUri.ToLower();
如果(!string.IsNullOrEmpty(_stroriginalurl))
{
Regex regurltest=new Regex(@“^http(s):/(www\)?(.*)\(bizo\.in)\/$”,RegexOptions.IgnoreCase);
if(regurltest.IsMatch(_stroriginalurl))
{
字符串_getparam=regurltest.Match(_stroriginalurl).Groups[3]。值;
如果(!\u getparam.Trim().Equals(“www”))
{
if(requestedPath.Trim()==“/”)
{
requestedPath=string.Format(“/{0}/home”,_getparam);
}
其他的
{
如果(!requestedPath.Contains(_getparam))
requestedPath=string.Format(“/{0}{1}”,_getparam,requestedPath);
}
}
}
}
//app.Context.Response.Write(requestedPath);
//app.Context.Response.Write(“”);
//app.Context.Response.Write(app.Request.Url.AbsoluteUri);
//app.Context.Response.End();
//获取配置规则
ReWriterRuleCollection rules=RewriterConfiguration.GetConfig().rules;
if(requestedPath.ToLower()包含(“css/”)| | requestedPath.ToLower()包含(“js/”)
返回;
if(requestedPath.ToLower()包含(“图像/”)| | requestedPath.ToLower()包含(“图像/”)
返回;
if(requestedPath.ToLower()包含(“.axd”))
返回;
if(requestedPath.ToLower()包含(“.axpx”))
返回;
if(requestedPath.ToLower()包含(“.ascx”))
返回;
如果(!IsExistsInPageList(requestedPath))
{
字符串_newrequestepath=CheckInDb_N_Replace(requestedPath);
如果(!string.IsNullOrEmpty(_newRequestPath))
{
如果(_newrequestepath.Contains(“未找到页面”))
{
如果(!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");
}
}
}
其他的
requestedPath=_newrequestepath;
}
}
//遍历每个规则。。。
for(int i=0;iprotected void Page_Load(object sender, EventArgs e)
{
form1.Action = Request.RawUrl;
}