Why I cannot use HttpResponseMessage class?

2019-09-11 07:56发布

问题:

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?

回答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.