对不起如果我把这个问题与任何人混淆,我正在努力使这个场景变得清晰。
我正在为一个调用HttpClient方法的方法编写测试用例。所以,我最后编写了以下测试方法。
[TestMethod]
public async Task CallComplexRefTypeParamAPI_Get_GetResponseWithParamatersNameValueAppended()
{
#region Arrange
//var resourceURL = @"http://localhost:32662/api/user/ComplexReferenceTypeParamStringResponse";
var resourceURL = @"/api/user/ComplexReferenceTypeParamStringResponse";
var restHelper = new RestHelper(_BaseAddress);
string ParameterKey1 = "VariableStr";
string ParameterValueStr = "Jia";
string ParameterKey2 = "VariableInt";
int ParameterValueInt = 1;
string ParameterKey3 = "VariableBool";
bool ParameterValueBool = true;
string ParameterKey4 = "VariableDateTime";
DateTime ParameterValueDateTime = DateTime.Now;
ComplexRefType ParameterComplexRefType = new ComplexRefType()
{
VariableBool = ParameterValueBool,
VariableDateTime = ParameterValueDateTime,
VariableInt = ParameterValueInt,
VariableStr = ParameterValueStr
};
string result;
#endregion
#region Act
using (WebApp.Start<WebApiStartup>(_BaseAddress))
{
restHelper.AddURLParameters("VariableComplexRef", ParameterComplexRefType);
restHelper.AddURLParameters("DummyStr", "DummyStr");
result = await restHelper.ExecuteAsync<string>(HttpMethod.Get, resourceURL);
}
#endregion
#region Assert
Assert.AreEqual<string>(string.Format("{0}={1}&{2}={3}&{4}={5}&{6}={7}",
ParameterKey1, ParameterValueStr,
ParameterKey2, ParameterValueInt,
ParameterKey3, ParameterValueBool,
ParameterKey4, ParameterValueDateTime), result);
#endregion
}
另一方面,我的测试控制器有以下两种方法。
public string GetMultipleTypeParamStringResponse(string VariableStr, int VariableInt, DateTime VariableDateTime)
{
return string.Format("VariableStr={0}&VariableInt={1}&VariableDateTime={2}", VariableStr, VariableInt, VariableDateTime);
}
public string GetComplexReferenceTypeParamStringResponse([FromUri]ComplexRefType VariableComplexRef, string DummyStr)
{
return string.Format("VariableStr={0}&VariableInt={1}&VariableBool={2}&VariableDateTime={3}",
VariableComplexRef.VariableStr,
VariableComplexRef.VariableInt,
VariableComplexRef.VariableBool,
VariableComplexRef.VariableDateTime);
}
我在Web API应用程序中复制了相同的Controller。如果运行测试方法,指向自托管API,应用程序将命中“GetMultipleTypeParamStringResponse”而不是“GetComplexReferenceTypeParamStringResponse”。但是,如果我针对Web API运行它,它会遇到合法的“GetComplexReferenceTypeParamStringResponse”方法。
有人请帮助我理解为什么会出现这种行为?在这两种情况下,生成的查询字符串看起来都很相似。自托管 http://localhost:8888/api/user/ComplexReferenceTypeParamStringResponse?VariableStr=Jia&VariableInt=1&VariableBool=True&VariableDateTime=1%2F5%2F2017下午3:49:10&amp; DummyStr = DummyStr
Web API http://localhost:32662/api/user/ComplexReferenceTypeParamStringResponse?VariableStr=Jia&VariableInt=1&VariableBool=True&VariableDateTime=1%2F5%2F2017下午3:50:58&amp; DummyStr = DummyStr
答案 0 :(得分:1)
将路线配置中的routetemplate更改为
routeTemplate: "api/{controller}/{action}/{id}"
使用显式Route属性和HttpGet动词标记方法,如下所示
[HttpGet]
[Route("api/user/ComplexReferenceTypeParamStringResponse")]