2012-04-03 3 views
3

Ich habe URL-Rewriting mit benutzerdefinierten Modulewriter in meiner asp.net-Anwendung verwendet und es funktioniert, aber das Problem ist, dass wenn ich auf eine Schaltfläche klicken Sie die Standard-URL im Browser zu etwas ändern wie http://tipl.bizo.in/Download.aspx?busid=tipl die durch http://tipl.bizo.in/tipl/Download ersetzt werden sholuld so wie kann ich klickenURL-Rewriting-Probleme auf Schaltfläche klicken in asp.net

dies auf die Schaltfläche erreichen

benutzerdefinierten Modul rewriter meinen Code für ist:

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

} 
+0

sollten Sie stattdessen asp.net routing versuchen. Verwenden Sie eine benutzerdefinierte Lösung zur Unterstützung von URLREWRITE? –

+0

aber das wird nicht generisch sein, so dass ich benutzerdefinierte Routing verwenden – iProgrammer

+0

was Sie brauchen, die nicht in Standard-Routing passt? –

Antwort

1

eigentlich nur umbenennen UrlRewrite die uRL der Anfrage aber asp.net tatsächlich weiß nichts über diese Veränderung. Sie müssen also etwas im Code ändern, damit Sie auf die Änderungen aufmerksam werden. Dies wird eine Lösung sein:

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

für weitere Informationen über das Problem und Lösung check this out:

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

Grüße.

+0

Was ist Form1? Ich verwende Inhaltsseite mit Masterseite. – yogihosting

+0

Es ist das Hauptformular auf der Seite definiert. Sie können das Formular mithilfe der Musterseiteninstanz finden. Überprüfen Sie http://StackOverflow.com/questions/178396/form-elements-in-asp-net-master-pages-and-content-pages –

Verwandte Themen