[英]Web API vs Self Hosted
Sorry If i confuse anyone with the question, I am trying hard to make the scenario clear. 抱歉,如果我使任何人困惑这个问题,我正在努力使情况变得清晰。
Am writing Test Cases for a method that invoke HttpClient Methods.So, I ended up writing following Test Method. 我正在为调用HttpClient Methods的方法编写测试用例。因此,我最终编写了以下测试方法。
[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
}
On other side, I have my Test Controller with following 2 methods. 另一方面,我的测试控制器具有以下2种方法。
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);
}
I have the same Controller replicated in an Web API application. 我在Web API应用程序中复制了相同的Controller。 If run the test method, pointing to the Self-Hosted API, the application hits "GetMultipleTypeParamStringResponse" instead of "GetComplexReferenceTypeParamStringResponse".
如果运行测试方法并指向自托管API,则应用程序将显示“ GetMultipleTypeParamStringResponse”而不是“ GetComplexReferenceTypeParamStringResponse”。 However, if I run it against the Web API, it hits the rightful "GetComplexReferenceTypeParamStringResponse" method.
但是,如果我针对Web API运行它,则会命中正确的“ GetComplexReferenceTypeParamStringResponse”方法。
Could someone please help me understand why this behavior ? 有人可以帮我理解为什么会这样吗? On both cases, the Query String generated looks to be similar.
在这两种情况下,生成的查询字符串看起来都是相似的。
Self Hosted http://localhost:8888/api/user/ComplexReferenceTypeParamStringResponse?VariableStr=Jia&VariableInt=1&VariableBool=True&VariableDateTime=1%2F5%2F2017 3:49:10 PM&DummyStr=DummyStr 自托管的http:// localhost:8888 / api / user / ComplexReferenceTypeParamStringResponse?VariableStr = Jia&VariableInt = 1&VariableBool = True&VariableDateTime = 1%2F5%2F2017 3:49:10 PM&DummyStr = DummyStr
Web API http://localhost:32662/api/user/ComplexReferenceTypeParamStringResponse?VariableStr=Jia&VariableInt=1&VariableBool=True&VariableDateTime=1%2F5%2F2017 3:50:58 PM&DummyStr=DummyStr Web API http:// localhost:32662 / api / user / ComplexReferenceTypeParamStringResponse?VariableStr = Jia&VariableInt = 1&VariableBool = True&VariableDateTime = 1%2F5%2F2017 3:50:58 PM&DummyStr = DummyStr
Change the routetemplate in your route configuration to 将路由配置中的routetemplate更改为
routeTemplate: "api/{controller}/{action}/{id}"
Mark the methods with explicit Route attribute and HttpGet verb like below 用显式Route属性和HttpGet动词标记方法,如下所示
[HttpGet]
[Route("api/user/ComplexReferenceTypeParamStringResponse")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.