自定義驗證規則can be written和一個規則可以從另一個派生。這讓我們創建一個規則,檢查是否需要響應url驗證,或者應該跳過特定請求。
[System.ComponentModel.Description(
"Validate response URLs. "
+ "Calls standard validation rule except when the 'Response URL' property is '-', "
+ "in which case no validation is done.")]
public class ResponseUrlWithSkip : ValidateResponseUrl
{
public override void Validate(object sender, ValidationEventArgs e)
{
if (string.IsNullOrEmpty(e.Request.ExpectedResponseUrl)
|| e.Request.ExpectedResponseUrl != "-")
{
base.Validate(sender, e);
}
else
{
e.WebTest.AddCommentToResult("Response URL validation skipped.");
}
}
}
上面的代碼檢查響應url屬性是否是一個連字符(即-
),並且如果它不是那麼標準響應URL規則被調用。 hyplen的選擇沒有什麼魔力。可以使用其他字符串,該字符串可以作爲驗證規則的屬性傳遞,方法是添加下面的聲明並測試其值而不是"-"
。
public string Skip { get; set; }
要使用此驗證規則,請從Web測試中刪除正常規則並添加此新規則的調用。