0

I have a method for my API controller

public class PhotoController : ApiController
{
    public IPhotoService _photoService;

    public PhotoController()
    {
        _photoService = new PhotoService();
    }

    [HttpPost]
    [Route("photo/upload/{photo}")]
    public bool UploadPicture()
    {
        try
        {
            var httpRequest = HttpContext.Current.Request;
            JObject param = JObject.Parse(HttpContext.Current.Request.Params["photo"]);

            var path = "../" + Utils.RandomGenerator() + Utils.RandomGenerator() + "." + httpRequest.Files[0].FileName.Split('.').Last();
            var postedFile = httpRequest.Files[0];
            var filePath = HttpContext.Current.Server.MapPath(path);
            postedFile.SaveAs(filePath);
            _photoService.UploadPhoto(param, path);

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}

If I remove this block of code

public IPhotoService _photoService;

    public PhotoController()
    {
        _photoService = new PhotoService();
    }

it work but whenever I create my service the API method can't work

on client console always print out this error

POST localhost:17699/photo/upload/%7Bphoto%7D 500 (Internal Server Error)

I don't know why because my others API controller can work smooth.

I've searched, cleaned and built project...etc...

Can you guys help me please

VuongNQ
  • 43
  • 1
  • 6
  • Your uploadPicture() method references `_photoService` object which you deleted. That's one reason. Putting that back in will still cause an error because you deleted the constructor that instantiated it. – Luminous Nov 03 '14 at 16:26
  • I know what you mean @Luminous, my service still there, I can run project without any error, my issue is uploadPicture API can't work – VuongNQ Nov 03 '14 at 16:39
  • Then why don't you put those deleted lines back in? Or if you don't want the constructor just put in `public IPhotoService _photoService = new PhotoService;`? – Luminous Nov 03 '14 at 16:46
  • I have done like your suggestion, remove constructor, create new service, and I got error like error in my question – VuongNQ Nov 04 '14 at 13:09

0 Answers0