Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ApiController could not be found

Tags:

Based on this video https://www.youtube.com/watch?v=IVvJX4CoLUY I have add the using System.Web; using System.Web.Http; but i still get the error state that apicontroller could no found, and so on, below picture is the error I face:

enter image description here

below is my code :

using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Collections.Generic;


namespace UploadToServer.Server.Controllers
{
public class UploadsController : ApiController
{
    [Route("api/Files/Upload")]
    public async Task<string> Post()
    {
        try
        {
            var httpRequest = HttpContext.Current.Request;

            if (httpRequest.Files.Count > 0)
            {
                foreach (string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];

                    var fileName = postedFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();

                    var filePath = HttpContext.Current.Server.MapPath("~/Uploads/" + fileName);

                    postedFile.SaveAs(filePath);

                    return "/Uploads/" + fileName;
                }
            }
        }
        catch (Exception exception)
        {
            return exception.Message;
        }

        return "no files";
    }
}

}

Anyone can share me ideas?

like image 710
Shi Jie Tio Avatar asked Mar 05 '18 14:03

Shi Jie Tio


People also ask

What is ApiController in .NET core?

ApiController attribute The [ApiController] attribute can be applied to a controller class to enable the following opinionated, API-specific behaviors: Attribute routing requirement. Automatic HTTP 400 responses. Binding source parameter inference. Multipart/form-data request inference.

What is an ApiController?

Web API Controller is similar to ASP.NET MVC controller. It handles incoming HTTP requests and send response back to the caller. Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder.

What is the difference between ApiController and controller?

They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data. ApiControllers are specialized in returning data.


1 Answers

You are missing the references in your project for those libraries. As per svdoever you can add the package Microsoft.AspNet.WebApi through NuGet to fix this. For instructions on how to use NuGet, please click here

like image 167
Luke Avatar answered Sep 21 '22 13:09

Luke