2019年7月25日 星期四

取得網站應用程式根網址

<%
    string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
%>

用法:
<script type="text/jscript">
    var someurl = '<%=baseUrl%>subpath/somepage.aspx';
</script>

透過 javascript 但不使用 window.open 來開啟網址

目的是為了避免被瀏覽器阻擋

function openURL(url, target) {
    var link = document.createElement("a");
    link.href = url;
    link.target = target;
    document.body.appendChild(link);
    link.onclick = function () {
        requestAnimationFrame(function () {
            document.body.removeChild(link);
            delete link;
        })
    };
    link.click();
}

openURL('http://www.google.com', '_new');

2019年7月8日 星期一

使用ag-grid 確保多人作業環境畫面永遠顯示最新資料

$scope.onTimeout = function () {
  if (!$scope.new) // 處於某種狀態暫時不更新
  {
      $http.post('...')
          .then(function (result) {
              var oldlist = Enumerable.From($scope.gridOptions.api.getModel().rowsToDisplay).Select('$.data');
              var newlist = Enumerable.From(result.data);
              var 增加資料s = [];
              newlist.ForEach(function (a) {
                  if (!oldlist.Any('$.id==' + a.id)) 增加資料s.push(a);
              });
              $scope.gridOptions.api.updateRowData({ add: 增加資料s, addIndex: 0 });
              var 減少資料s = [];
              oldlist.ForEach(function (a) {
                  if (!newlist.Any('$.id==' + a.id)) 減少資料s.push(a);
              });
              $scope.gridOptions.api.updateRowData({ remove: 減少資料s });                    Enumerable.From($scope.gridOptions.api.getModel().rowsToDisplay).ForEach(function (node) {
                  var newdata = newlist.Where('$.id==' + node.data.id).SingleOrDefault();
                  if (newdata && JSON.stringify(node.data) != JSON.stringify(newdata)) {
                    node.setData(newdata);
                    $scope.gridOptions.api.redrawRows({ rowNodes: [node] });
                  }
              });
          }).finally(function (data) {
              $timeout($scope.onTimeout, 10000);
          });
  }
  else $timeout($scope.onTimeout, 10000);
}

$timeout($scope.onTimeout, 10000);

頁面內容不要cache 確保每次都抓最新資料

Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);  // HTTP 1.1.
Response.Cache.AppendCacheExtension("no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
Response.AppendHeader("Expires", "0"); // Proxies.

※未經驗證

2019年7月6日 星期六

修改 line 畫面尺寸超過200%

開啟 C:\Users\[使用者帳號]\AppData\Local\Line\Data\LINE.ini
找到 scaleRatio=? (2=200%,3=300%)
若無此設定則手動添加

2019年7月5日 星期五

webmethod 回傳錯誤

var httpContext=System.Web.HttpContext.Current;
httpContext.Response.Write(錯誤訊息);
httpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
httpContext.Response.Flush();
httpContext.Response.SuppressContent = true;
httpContext.ApplicationInstance.CompleteRequest();

※若為 async 函數,await 後就無法再取得 System.Web.HttpContext.Current,必須先用變數記錄起來,await 之後再填回
var httpcontext = HttpContext.Current;
await xxxAsync().ConfigureAwait(false);
HttpContext.Current = httpcontext;

2019年7月1日 星期一

加密 web.config 避免敏感資料外洩

例如針對區段 connectionStrings 進行加密

aspnet_regiis -pef "connectionStrings" "[web.config 所在路徑]"

若為app.config 則須先修改成 web.config

※必須在執行應用程式所屬電腦上進行加密,否則會無法正確解密

主控台程式不顯示dos 視窗及 main 不要結束

不顯示dos 視窗

main 不要結束
====
最後呼叫  Console.ReadLine();
此時會等待使用者按下enter 才往下,因此不會直接結束程式
搭配"不顯示dos 視窗"就不會誤按enter 而結束程式

Entity Framework 建立新物件並儲存後馬上取得關聯資料

使用 CreateProxy 建立物件,不要直接 new var newmodel = _contextXXX.CreateProxy<yyy>(); ... _contextXXX.yyy.Add(newmodel); await _contextXXX.SaveC...