目前分類:ASP.NET (11)

瀏覽方式: 標題列表 簡短摘要

今天與同事在查找一個 ASP.NET 1.0 應用程式無法將資訊寫入 Event log.

它是再安裝在 Windows Server 2003 (IIS6)

一開始很自然的檢查 ApplicationPool 的設定, 是 Network Service

 

那... 為什麼會出問題呢? 一直找不出為什麼?

後來同事寫了一個網頁顯示目前執行的 WindowsIdentity 的 Name 為誰

發是是 IUSR_<computer> ?? 那會按內 ??

不過這也提供了一個很重要的線索

再仔細檢查一下 Web.config, 發現 <identity impersonate="true"/> , 對 … 就是它了

把它改為 <identity impersonate="false"/> 即會是用 Network Service 的身份在執行

可以用下列的表格快速檢查一下ASP.NET 及 IIS 的設定是否有問題

Impersonation

Anonymous Access

WindowsIdentity.GetCurrent().Name

User.Identity.Name

Enabled

Enabled

IUSR_<computer>

Empty string

Enabled

Disabled

ISV\Jacky

ISV\Jacky

Disabled

Enabled

NT Authority\Network Service

Empty string

Disabled

Disabled

NT Authority\Network Service

ISV\Jacky

Enjoy.

anISV 發表在 痞客邦 PIXNET 留言(1) 人氣()

這是專案最近遇到的一個問題,還是將它記錄下來以免忘記.

若您是使用純 .NET Solution 的朋友,可以忽略它,因為 .NET 程式本身呼叫是不會有這個問題的.

 

在一個大型的專案中,Web Services 都很容易會有兩台以上的機器的部署.

架構是 Client => L4 Switch => Web Services => Database (Cluster)

https 到 L4 switch 後轉為 http 到 web services,所以 web services 收到的為 http 的 request

所以它在回給前端的 WSDL 中的 soap:address location=”http://…”

Client 收到的 URL 為 http,再拿這 URL 來呼叫即會被 L4 Switch 擋下來,因為它只允許 https 來連接

可以透過三步驟將其替換為 https

1. 新增一個 class,繼承自 SoapExtensionReflector並 override ReflectMethod

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Description;

namespace ChangeSoapAddressDemo
{
    public class SoapAddressReflector : SoapExtensionReflector
    {
        public override void ReflectMethod()
        {
            ServiceDescription sd = ReflectionContext.ServiceDescription;

            foreach (Service service in sd.Services)
            {
                foreach (Port port in service.Ports)
                {
                    foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
                    {
                        SoapAddressBinding address = (SoapAddressBinding)extension;
                        address.Location = RemapHttpReferencesToHttps(address.Location);
                    }
                }
            }
        }

        private string RemapHttpReferencesToHttps(string location)
        {
            return location.Replace("http:", "https:");
        }
    }
}

 

2. 在 web.config 中加入下列的設定

<webServices>
        <soapExtensionReflectorTypes>
          <add type="ChangeSoapAddressDemo.SoapAddressReflector, ChangeSoapAddressDemo"/>
        </soapExtensionReflectorTypes>
      </webServices>

 

3. 瀏覽 WSDL,即可看到 soap:address location=”https://…”

image

Enjoy

anISV 發表在 痞客邦 PIXNET 留言(0) 人氣()

因為最近專案遇到一些問題,發現在程式 Content page 的 Page_Load 會被 Master page 的Page Laod 給蓋過,所以查了一下 Master Page 與 Content Page 一些事件的執行順序:

Master page controls Init event
Content controls Init event


Master page Init event
Content page Init event

Content page Load event
Master page Load event

Content page PreRender event
Master page PreRender event

Master page controls PreRender event
Content controls PreRender event

 

Hope this helps.

anISV 發表在 痞客邦 PIXNET 留言(0) 人氣()

在 ASP.NET 的網頁將 Grid 資料轉 Excel 檔格式供使用者下載,這是一個很常用的功能,

在自己的測試環境也測了 N 次,都沒有問題,但一將系統放到 Stage 的環境就無法使用,

得到的錯誤訊息為:

Internet Explorer 無法從 server 下載 file
Internet Explorer 無法開啟這個網際網路網站。可能是因為要求的網站無法使用或找不到。請稍後再試。

 

且用 FireFox 還是可以正常下載 Excel 檔,只有 IE 會有問題

心想這有可能是環境不同上的一些相關,因為 Stage 環境要使用 HTTPS.

於是搜尋了一下,果然,找到了一篇文章 Internet Explorer 無法從 SSL 網站開啟 Office 文件

剛好我們因某些需求將 ASP.NET 的網頁設為 No=Cache,再加上 SSL 即造成這個情況

解決的方式:

即是將這一個頁面不設定為 No-Cache

Hope this helps.

anISV 發表在 痞客邦 PIXNET 留言(0) 人氣()

ASP.NET MVC 2 is a framework for developing highly testable and maintainable Web applications by leveraging the Model-View-Controller (MVC) pattern. The framework encourages developers to maintain a clear separation of concerns among the responsibilities of the application – the UI logic using the view, user-input handling using the controller, and the domain logic using the model. ASP.NET MVC applications are easily testable using techniques such as test-driven development (TDD).


The installation package includes templates and tools for Visual Studio 2008 SP 1 to increase productivity when writing ASP.NET MVC applications. For example, the Add View dialog box takes advantage of customizable code generation (T4) templates to generate a view based on a model object. The default project template allows the developer to automatically hook up a unit-test project that is associated with the ASP.NET MVC application.

Because the ASP.NET MVC framework is built on ASP.NET 3.5 SP 1, developers can take advantage of existing ASP.NET features like authentication and authorization, profile settings, localization, and so on.

 

Download:

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=3b537c55-0948-4e6a-bf8c-aa1a78878da0

anISV 發表在 痞客邦 PIXNET 留言(1) 人氣()

 

Problem:

之前在專案中, 有測到一個疑似一個 .NET Framework Regex 的 bug?

例如使用 PATTERN = “\w”

\w表示任何一個字元與數字以及 '_' ,意同 [a-zA-Z0-9_]

輸入全形英數字及中文字可以通過在 Server side 的 Regular expression 檢查

但相同的 pattern 在 Javascript 的 Regular expression 是不會過的

最近找到答案了…

Root Cause:

    Server side RegExp uses a different regular expression engine than JavaScript.

Solution:

    You can get compatible behavior by using a RegexOptions.EcmaScript with your Regex.

    Regex regexp = new Regex("\\w", RegexOptions.ECMAScript);

    For more information:

    RegexOptions 列舉型別

    http://msdn.microsoft.com/zh-tw/library/system.text.regularexpressions.regexoptions.aspx

    ECMAScript vs. Canonical Matching Behavior

    http://msdn.microsoft.com/en-us/library/04ses44d(VS.71).aspx

Sample:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RegExpApp._Default" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function testRegExp()
{
var email = document.getElementById("<%=txtEmail.ClientID %>").value;
var regexp = new RegExp("\\w");
if (email.match(regexp)) {
alert("Successful match");
} else {
alert("No match");
}
}
</script>


<script runat="server">
protected void btnNETRegExp_Click(object sender, EventArgs e)
{
Regex regexp;

if (chkECMAScript.Checked)
{
regexp = new Regex("\\w", RegexOptions.ECMAScript);
}
else
{
regexp = new Regex("\\w");
}

if(regexp.IsMatch(txtEmail.Text))
{
Response.Write("Successful match");
}
else{
Response.Write("No match");
}

}


</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnNETRegExp" runat="server" Text="NET RegExp" OnClick="btnNETRegExp_Click" />
&nbsp;&nbsp;&nbsp;
<asp:CheckBox ID="chkECMAScript" runat="server" />
<br />
<br />
<input id="btnJSRegExp" type="button" value="JavaScript RegExp" onclick="testRegExp();" /></div>
</form>
</body>
</html>

 

Hope this helps.

anISV 發表在 痞客邦 PIXNET 留言(0) 人氣()

 

這也是在專案上遇到的一個問題,目前可以對 DropDownList 控制項設定 Tooltip,但無法直接從 VS 的 IDE 屬性頁直接設定。

所以筆者用以下程式碼來對每一個項目加入 Tooltip:

 

for (int i = 0; i < yourDropDownList.Items.Count; i++)
{
   yourDropDownList.Items[i].Attributes.Add("title", yourDropDownList.Items[i].Text);
}

 


其效果就是滑鼠移到每一個項目上時,即會出現 Tooltip 的說明。


 


Enjoy.

anISV 發表在 痞客邦 PIXNET 留言(0) 人氣()

前一陣子在專案中,為了讓使用者可以更清楚的知道自己所選取的項目為何,所以筆者必須將 RadioButtonList 中被選取的選項加上粗體的樣式。
本想應該是很簡單,只要把 RadioButtonList 中的項樣的樣式設定即可,但是
竟發現 RadioButtonList 並未提供針對其每個選項設定樣式的功能。XD…
也可以從產生出來的網頁的原始碼發現 RadioButtonList 產生的程式碼為 <label for=”……”>XXX</label> 的寫法。如下:
 
<table id="RadioButtonList1" border="0">
	<tr>
	  <td><input id="RadioButtonList1_0" type="radio" name="RadioButtonList1" value="選項一" /><label for="RadioButtonList1_0">選項一</label></td>
	</tr>
        <tr>
	  <td><input id="RadioButtonList1_1" type="radio" name="RadioButtonList1" value="選項二" /><label for="RadioButtonList1_1">選項二</label></td>
	</tr>
        <tr>
	  <td><input id="RadioButtonList1_2" type="radio" name="RadioButtonList1" value="選項三" /><label for="RadioButtonList1_2">選項三</label></td>
	</tr>
</table>

 


試了一些方法後,最後筆者用一個 div 將 RadioButtonList 放在其中,再用 JavaScript 來描這個 div 中的 label 的 Tag


程式碼如下:


 

    function MakeBold(divObj, elementID) {
        var labels = divObj.getElementsByTagName("label");
        for(var i=0;i<labels.length;i++) 
        {
            if(labels.item(i).htmlFor == elementID) 
            {
                labels.item(i).style.fontWeight="bold";
            }
            else
            {
                labels.item(i).style.fontWeight="normal";
            }
        }
    }

 


P.S. 請記得在每個選項加入 onclick 的事件來觸發它。


Hope this helps.

anISV 發表在 痞客邦 PIXNET 留言(1) 人氣()

ASP.NET MVC 1.0 provides a new Model-View-Controller (MVC) framework on top of the existing ASP.NET 3.5 runtime. This means that developers can take advantage of the MVC design patterns to create their Web Applications which includes the ability to achieve and maintain a clear separation of concerns (the UI or view from the business and application logic and backend data), as well as facilitate test driven development (TDD). The ASP.NET MVC framework defines a specific pattern to the Web Application folder structure and provides a controller base-class to handle and process requests for “actions”. Developers can take advantage of the specific Visual Studio 2008 MVC templates within this release to create their Web applications, which includes the ability to select a specific Unit Test structure to accompany their Web Application development.

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b

anISV 發表在 痞客邦 PIXNET 留言(0) 人氣()

The ASP.NET MVC RC2 release provides a new Model-View-Controller (MVC) framework on top of the existing ASP.NET 3.5 runtime. This means that developers can take advantage of the MVC design patterns to create their Web Applications which includes the ability to achieve and maintain a clear separation of concerns (the UI or view from the business and application logic and backend data), as well as facilitate test driven development (TDD). The ASP.NET MVC framework defines a specific pattern to the Web Application folder structure and provides a controller base-class to handle and process requests for “actions”. Developers can take advantage of the specific Visual Studio 2008 MVC templates within this release to create their Web applications, which includes the ability to select a specific Unit Test structure to accompany their Web Application development.

下載位置:

http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=ee4b2e97-8a72-449a-82d2-2f720d421031

 

Enjoy.

anISV 發表在 痞客邦 PIXNET 留言(0) 人氣()

The ASP.NET MVC RC1 release provides a new Model-View-Controller (MVC) framework on top of the existing ASP.NET 3.5 runtime. This means that developers can take advantage of the MVC design patterns to create their Web Applications which includes the ability to achieve and maintain a clear separation of concerns (the UI or view from the business and application logic and backend data), as well as facilitate test driven development (TDD). The ASP.NET MVC framework defines a specific pattern to the Web Application folder structure and provides a controller base-class to handle and process requests for “actions”. Developers can take advantage of the specific Visual Studio 2008 MVC templates within this release to create their Web applications, which includes the ability to select a specific Unit Test structure to accompany their Web Application development.

 

The MVC framework is fully extensible at all points, allowing developers to create sophisticated structures that meet their needs, including for example Dependency Injection (DI) techniques, new view rendering engines or specialized controllers.

 

As the ASP.NET MVC framework is built on ASP.NET 3.5, developers can take advantage of many existing ASP.NET 3.5 features, such as localization, authorization, Profile etc.

 

Note: A couple of issues have been reported with the RC. We will post a refresh on Friday, January 30, 2009

 

下載位置:http://www.microsoft.com/downloads/details.aspx?FamilyID=f4e4ee26-4bc5-41ed-80c9-261336b2a5b6&DisplayLang=en#filelist

 

Enjoy.

anISV 發表在 痞客邦 PIXNET 留言(0) 人氣()