asp.net中点击按钮时的url重写问题



我在我的asp.net应用程序中使用了使用自定义模块重写器的url重写,它正在工作,但问题是,当我单击任何按钮时,浏览器中的默认url都会更改为类似http://tipl.bizo.in/Download.aspx?busid=tipl应该由http://tipl.bizo.in/tipl/Download那么我该如何实现点击按钮

我的自定义模块重写器代码是:

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; 
    }
}

实际上urlrewrite只重命名了请求的url,但asp.net实际上对这个更改一无所知。因此,您必须更改代码中的某些内容,以使页面了解更改。这将是一个解决方案:

protected void Page_Load(object sender, EventArgs e)  
{  
    form1.Action = Request.RawUrl;  
}  

有关问题和解决方案的更多信息,请查看:

http://ruslany.net/2008/10/aspnet-postbacks-and-url-rewriting/

谨致问候。

最新更新