I have got the solution to the question, thanks to the author of the post @JayChase
I add a new method to IPhotoManager:
Task<PhotoViewModel> Get(string fileName);
The concrete implementation would be similar to the existing get but returning a single PhotoViewModel and having the query filter by filename as well:
public async Task<PhotoViewModel> Get(string fileName)
{
PhotoViewModel photo = new PhotoViewModel();
DirectoryInfo photoFolder = new DirectoryInfo(this.workingFolder);
await Task.Factory.StartNew(() =>
{
photo = photoFolder.EnumerateFiles()
.Where(fi => new[] { ".jpg", ".bmp", ".png", ".gif", ".tiff" }.Contains(fi.Extension.ToLower())
&& fi.Name == fileName
)
.Select(fi => new PhotoViewModel
{
Name = fi.Name,
Created = fi.CreationTime,
Modified = fi.LastWriteTime,
Size = fi.Length / 1024
})
.FirstOrDefault();
});
return photo;
}
Finally add the Get(string filename) method to the photo api:
public async Task<IHttpActionResult> Get(string fileName)
{
var result = await photoManager.Get(fileName);
return Ok(new { photo = result });
}
Moreso, I have followed your sample project and also implement the getById method from the WebAPI and all work fine from you sample project, i try to replicate the project from a fresh ASP.Net Empty Web APi, after following the step in creating the project by copig the exact code to a new project, on testing the services, GetAll and POST Method work well, but the GetById and DELETE method do return 404 error but in your sample project all works well,
http://prntscr.com/6vp958
But if i point to a wrong filename i get an empty array of photo: http://prntscr.com/6vpa4q
I resolved the issues by adding
<!--add below sectio to allow DELETE method-->
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
to the web.config file
The authour also comment:
If you are still getting 404 or 405 errors when trying to call the new get method then you may need to add the route attribute like below:
[Route("{fileName}")]
public async Task<IHttpActionResult> Get(string fileName)
{
...
}
Thanks all for the solution to the problem, hope this will help someone
Thanks.