locked
The type or namespace name 'FromBody' could not be found (are you missing a using directive or an assembly reference? RRS feed

  • Question

  • User-1980778524 posted

    Hi I did the following:

    I am using VS2015
    I added a Class Library into my Web Api Project
    I added a reference for this ClassLibrary in the web Api Project
    I added the Ado.net Entities DataModel in the ClassLibrary

    I got an error  when compiling :

    The type or namespace name 'FromBody' could not be found (are you missing a using directive or an assembly reference?)


    -- these are in the Class Library

    1) ICustomerRepository

        public interface ICustomerRepository
        {
            IEnumerable<Customer> GetAll();
            User GetById(int CustId);
            int Update(Customer cust);
            void Add(Customer custinfo);
            void Delete(int CustId);
        }


    2) CustomerRepository
       
    public class CustomerRepository :ICustomerRepository
        {
        
            MyEntities ctx = new MyEntities();    

            public IEnumerable<Customer> GetAll()
            {    
                return ctx.Customers;
            }

            public Customer GetById(int contactId)
            {
                IQueryable<Customer> customers = ctx.Customers.Where(a => a.CustId == contactId);
                return Customers.FirstOrDefault();
            }      


    //problem :

            public void Add([FromBody]Customer contact)
            {       
                var addedContact = ctx.Customers.Add(contact);
                ctx.SaveChanges();
        
            }  

        }

    Wednesday, April 26, 2017 5:28 AM

Answers

  • User-2057865890 posted

    Hi SkyDriving,

    It is not available since the code is inside a ClassLibrary project.  I added references like system.Net/System.net.Http, there is no System.web.Http in ClassLibrary project.

    You need the Microsoft.AspNet.WebApi.Core package.

    To install Microsoft ASP.NET Web API 2.2 Core Libraries, run the following command in the Package Manager Console

    Install-Package Microsoft.AspNet.WebApi.Core

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 26, 2017 7:27 AM

All replies

  • User-1509636757 posted

    SkyDriving

            public void Add([FromBody]Customer contact)

    Please refer: c# - Am I being too explicit in passing an HttpWebRequest object to a Web API method?

    Wednesday, April 26, 2017 6:00 AM
  • User-2057865890 posted

    Hi SkyDriving,

    The type or namespace name 'FromBody' could not be found (are you missing a using directive or an assembly reference?

    You need to add:

    using System.Web.Http;

    at the top of your file before you can use [FromBody].

    Best Regards,

    Chris

    Wednesday, April 26, 2017 6:56 AM
  • User-1980778524 posted

    Hi SkyDriving,

    Hi Chris,

    1) I have changed the spelling mistake. The result still the same.

    2) I tried to add : using System.web.Http

    It is not available since the code is inside a ClassLibrary project.  I added references like system.Net/System.net.Http, there is no System.web.Http in ClassLibrary project.

    SkyDriving

    The type or namespace name 'FromBody' could not be found (are you missing a using directive or an assembly reference?

    You need to add:

    using System.Web.Http;

    at the top of your file before you can use [FromBody].

    Best Regards,

    Chris

    Wednesday, April 26, 2017 7:19 AM
  • User-1980778524 posted

    Hi all,

    I used another approach.  I am putting all the classes for Repository-pattern to access SQL Server in the Web Api Project. This is ok. There is no build error.

    I would like to know why adding another ClassLIbrary project with all the classes for repository  to the Web Api project unable to work?

    Wednesday, April 26, 2017 7:25 AM
  • User-2057865890 posted

    Hi SkyDriving,

    It is not available since the code is inside a ClassLibrary project.  I added references like system.Net/System.net.Http, there is no System.web.Http in ClassLibrary project.

    You need the Microsoft.AspNet.WebApi.Core package.

    To install Microsoft ASP.NET Web API 2.2 Core Libraries, run the following command in the Package Manager Console

    Install-Package Microsoft.AspNet.WebApi.Core

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 26, 2017 7:27 AM
  • User-1980778524 posted

    Thank you chris.

    There are two:

    1) Microsoft.aspNet.WebApi.core

    2)Microsoft.aspNet.webApi

    I selected the (1). But using (2) is Ok right ?

    Thanks

    Wednesday, April 26, 2017 8:18 AM