Pergunta

I am writing a Web API project (MVC architecture). I have a utility class with methods that returns an HttpResponseMessage. If this class is placed in a Utility folder everything works. If I attempt to place the class within the App_Code folder, I receive the message "The type or namespace name 'HttpResponseMessage' found not be found."

An example of a method that this occurs with is provided below. What needs to be done to be able to declare a variable or method as HttpResponseMessage within the App_Code folder?

    public HttpResponseMessage GetResponseMessage<T>(T item, MediaTypeFormatter mtFormatter)
    {
        HttpResponseMessage response = new HttpResponseMessage()
        {
            Content = new ObjectContent<T>(item, mtFormatter)
        };

        return response;
    }
Foi útil?

Solução

Files outside of App_Code are compiled when Visual Studio calls the C# compiler and passes it the appropriate referenced assemblies (DLLs) based on the references in the csproj file.

Files inside App_Code are compiled when ASP.NET at runtime calls the C# compiler and passes it the appropriate referenced assemblies (DLLs) based on the reference in the web.config file.

To add a reference to System.Net.Http (the assembly where HttpResponseMessage is located), open your web.config file and merge in this setting:

<compilation debug="true" targetFramework="4.5">
    <assemblies>
        <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </assemblies>
</compilation>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top