我正在通过一个在线教程(构建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?)
Run Code Online (Sandbox Code Playgroud)
我试图包含几个名称空间(如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);
}
}
Run Code Online (Sandbox Code Playgroud)
你不能拥有using System.Uri;,因为Uri是一个类(并且在Visual Studio 2015之前不允许静态导入,我认为你现在不使用它,即使在VS 2015中,它也会因为Uri不是静态类而失败)
包括:
using System;
Run Code Online (Sandbox Code Playgroud)
并删除
using System.Uri;
Run Code Online (Sandbox Code Playgroud)