为什么我不能使用HttpResponseMessage类? [英] Why I cannot use HttpResponseMessage class?

查看:179
本文介绍了为什么我不能使用HttpResponseMessage类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET Web API的新手,并且想从我制作的实用工具类中创建 HttpResponseMessage 实例.然后我做了一个非常简单的课.

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:类型或名称空间名称"HttpResponseMessage"不能为找到(您是否缺少using指令或程序集引用?)

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 可从Controller类获得,该类由ASP.NET自动创建,但不是我的Utility类.

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

我想念什么?

推荐答案

我注意到您将类放在项目的 App_Code 文件夹中.此文件夹在ASP.NET世界中具有特殊用途,应为用于要作为应用程序的一部分进行编译的共享类和业务对象.因此,要么将您的类移到另一个文件夹,要么在属性部分将其 Build Action 更改为 Compile .

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.

这篇关于为什么我不能使用HttpResponseMessage类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆