0

I'm new to ASP.NET Web API and want to make HttpResponseMessage instance from a utility class I made. Then I made very simple class.

But following compile error occurred.

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;

namespace myapplication.App_Code.Utils
{
    public class HttpUtility
    {
        // compile error here
        public HttpResponseMessage GetHttpResponseMessage ()
        {
            return new HttpResponseMessage();
        }
    }
}

HttpResponseMessage is available from Controller Class which was made automatically by ASP.NET but not my Utility class.

What am I missing?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Master.D
  • 19
  • 1
  • 5
  • Is this class placed in a different project than your API project? – Izzet Yildirim Jan 20 '17 at 07:22
  • You can use it, provided you add the appropriate assembly reference. The error message explains as much. Check [the documentation](https://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.118).aspx) for the class and add the appropriate assembly. It's right next to the bold `Assembly:` tag right after the heading – Panagiotis Kanavos Jan 20 '17 at 09:00
  • @IzzetYildirim it is same project. – Master.D Jan 20 '17 at 09:30
  • The assembly is "System.Net.Http.dll" and it is already referenced, but it is only available only from Controller class, not my utility class – Master.D Jan 20 '17 at 09:39

1 Answers1

1

I have noticed that you placed your class in App_Code folder in your project. This folder has a special purpose in ASP.NET world and should be used for shared classes and business objects that you want to compile as part of your application. So either move your class into another folder, or change its Build Action in properties section to Compile.

Izzet Yildirim
  • 640
  • 1
  • 11
  • 19