从调用Web Api 2异步服务的控制台应用程序执行测试时,出现404错误(未找到)错误。我使用的是EF 4.5.1和MVC。我有两个相同的控制器,一个是异步(见下文),另一个不是。
此通话有效:http://api.ptagis.org/tagdata/3D9.1C2D927A1B
这不是:http://api.ptagis.org/tagdataasync/3D9.1C2D927A1B
这里我试图测试Web Api异步服务:
[Route("{tag}")]
public async Task<TaggingEvent> GetTaggingEventByTagAsync(string tag)
{
TaggingEvent tagEvent = null;
if (IsValidTagCode(tag))
{
tagEvent = await context.TaggingEvents.FindAsync(tag);
if (tagEvent == null)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No tag found matching = {0}", tag)),
ReasonPhrase = "Tag Code Not Found"
};
throw new HttpResponseException(resp);
}
}
else
{
var resp = new HttpResponseMessage(HttpStatusCode.NotAcceptable)
{
Content = new StringContent(string.Format("Invalid tag code = {0}", tag)),
ReasonPhrase = "Invalid PIT Tag Code Format"
};
throw new HttpResponseException(resp);
}
return tagEvent;
}
使用此控制台应用程序:
class Program
{
static void Main(string[] args)
{
RunAsync().Wait();
}
static async Task RunAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://api.ptagis.org/");
HttpResponseMessage response = await client.GetAsync("tagdataasync/3D9.1C2D927A1B");
if (response.IsSuccessStatusCode)
{
TaggingEvent tag = await response.Content.ReadAsAsync<TaggingEvent>();
Console.WriteLine("{0}\t${1}\t{2}", tag.Tag, tag.Species, tag.Run);
}
}
}
}
任何想法/想法/建议?
谢谢
更多信息:我的路线是:[RoutePrefix(“tagdata”)]
我的代码同步如下所示:
[Route("{tag}")]
public TaggingEvent GetTaggingEventByTag(string tag)
{
TaggingEvent tagEvent = null;
if (IsValidTagCode(tag))
{
tagEvent = context.TaggingEvents.Find(tag);
if (tagEvent == null)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No tag found matching = {0}", tag)),
ReasonPhrase = "Tag Code Not Found"
};
throw new HttpResponseException(resp);
}
}
else
{
var resp = new HttpResponseMessage(HttpStatusCode.NotAcceptable)
{
Content = new StringContent(string.Format("Invalid tag code = {0}", tag)),
ReasonPhrase = "Invalid PIT Tag Code Format"
};
throw new HttpResponseException(resp);
}
tracker.TrackPageView("GetTaggingEventByTag", "tagdata");
return tagEvent;
}