找不到类型或命名空间名称 'Uri':(包括 System.Uri 命名空间)

3

我正在通过一个在线教程学习ASP.NET(构建Web应用程序)。问题是当我重复使用导师的代码时,我遇到了这个错误:

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

我尝试包含多个命名空间(例如System.Uri),但它们都无法工作。有人知道问题出在哪里,以及我需要做什么来解决这个问题吗?
以下是代码:
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Web;
using System.Web.Http;
using System.Net.Http;
using System.Data.Entity.Infrastructure;
using System.Uri;
        public HttpResponseMessage Post(Friend friend)
        {
            if (ModelState.IsValid)
            {
                db.Friends.Add(friend);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, friend);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = friend.FriendId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
    }
2个回答

11

由于Uri是一个类,所以你不能使用using System.Uri;(在Visual Studio 2015之前不允许静态导入,我想你现在也没有在使用VS 2015,即使在VS 2015中,它也会失败,因为Uri不是静态类)。

包括:

using System;

并移除

using System.Uri;

2
谢谢,伙计。你救了我的一天。7分钟后我会标记为答案。祝福你。 - undefined

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接