Asp.NET專案客製化方式

  • 6248
  • 0

Asp.NET專案客製化方式

前言

我們的工作是依現有的產品再加上客製化給客戶,客製化之後,如果產品有升級,要升級原有的程式總是要花很多的氣力及時間來調整!

研究&實作

在ASP時代,我們是判斷客製化的目錄是否有程式,有的話,就轉到客製化的目錄去執行該程式!

這樣的話,產品的程式就可以Copy過去而不用去避掉客製化的程式!

在ASP.NET,我們似乎也可以這樣搞! 所以在Global.asax中的Application_BeginRequest事件中,來檢查是否有客製化的程式!

//客製化的目錄名稱為Custom
const string CustomFolderName = "Custom";
protected void Application_BeginRequest(object sender, EventArgs e)
{
  if (Request.HttpMethod == "GET")
  {
    string reqPath = Request.Url.AbsolutePath.ToLower();
    bool isAspx = reqPath.EndsWith(".aspx");
    //檢查Customize的目錄是否有相同的程式,有的話,就導過去!
    string custFolderPath = "/" + CustomFolderName + "/" + System.IO.Path.GetFileName(reqPath);
    if(Request.Url.AbsolutePath.EndsWith(custFolderPath, true, null) == false) {
      string customFilePath = Request.ApplicationPath + custFolderPath;
      string customPhyFilePath = Server.MapPath(customFilePath);
      if (System.IO.File.Exists(customPhyFilePath))
      {
        if (isAspx)
        {
          //是ASPX File 
          Response.Redirect(customFilePath + "?" + Request.QueryString.ToString());
        }
        else
        {
          //非ASPX File
          Server.Transfer(customFilePath, true);
        }
      } 
    }
  }
}

要區分程式是否為.aspx,因為如果全都使用Server.Transfer的話,有時會有「Sys.WebForms.PageRequestManagerServerErrorException: Viewstate MAC 的驗證失敗。如果此應用程式是由 Web 伺服陣列或叢集所裝載,請確定 <machineKey> 組態指定有相同的 validationKey 和驗證演算法。AutoGenerate 無法在叢集中使用。」的錯誤!

image

image

另外,客製化的aspx.vb的class name要與產品的class name不同才不會產生錯誤哦!

image

結論

目前只是使用簡單的測試專案,還有其他的問題需要解決!如客製App_GlobalResources Resource就必須要用Merge,客製UserControl則需要客製有用到的相關程式,因為產品參考UserControl的Path是設定在aspx上!

如有進一步的資訊,再跟大家分享。也希望大家能給能予其他的做法或想法,謝謝!

範例程式

CustomizeIssue.rar

 

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^