我通过遵循一个在线教程(构建Web应用程序)来学习ASP.NET。问题是当我重用教师代码时,我停留在此错误上:
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);
}
}
最佳答案
您不能拥有using System.Uri;
,因为Uri
是一个类(并且直到Visual Studio 2015才允许静态导入,我想您现在不使用它,即使在VS 2015中,由于Uri
不是静态类)
包括:
using System;
并删除
using System.Uri;