2012-04-03 17 views
3

私のasp.netアプリケーションでカスタムのmodulerewriterを使用してURLの書き換えを行っていますが、問題はそのボタンをクリックするとブラウザのデフォルトのURLが何かに変更されます以下のようなので、どのように私はこれを達成することができ、ボタンにカスタムモジュール書き換え用URLをasp.netのボタンクリックで書き直しました

私のコードをクリックしhttp://tipl.bizo.in/tipl/Downloadに置き換えることがsholuld http://tipl.bizo.in/Download.aspx?busid=tiplは次のとおりです。

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

代わりにasp.netルーティングを試してください。 urlrewriteをサポートするカスタムソリューションを使用していますか? –

+0

しかし、これは一般的ではありませんので、私はカスタムルーティングを使用しています – iProgrammer

+0

あなたはそれがデフォルトルーティングに収まらないことがありますか? –

答えて

1

実際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/

よろしくこれが解決策になります。

+0

form1とは何ですか?私はマスターページでコンテンツページを使用しています。 – yogihosting

+0

メインフォームがページに定義されています。マスターページインスタンスを使用してフォームを見つけることができます。 http://stackoverflow.com/questions/178396/form-elements-in-asp-net-master-pages-and-content-pagesを確認してください。 –