大家好,我试图用ssms和visual studio制作一个api。我看到了一个教程,尽管我完全正确地执行了步骤,但返回的消息是“找不到与请求URI匹配的http资源”:
我的控制器中有这样的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using EmployeeDataAccess;
namespace EmployeeService1.Controllers
{
public class EmployeeController : ApiController
{
public IEnumerable<Signage> Get()
{
using (Raw_DataEntities entities = new Raw_DataEntities())
{
return entities.Signages.ToList();
}
}
public Signage Get(int fid)
{
using (Raw_DataEntities entities = new Raw_DataEntities())
{
return entities.Signages.FirstOrDefault(e => e.FID == fid);
}
}
}
}
WebApiConfig
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace EmployeeService2
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
我做错了什么?当我尝试在指定的本地主机中路由时,将返回以下内容:
System.InvalidOperationException HResult = 0x80131509 消息=在应用程序配置文件中找不到名为'Raw_DataEntities'的连接字符串。 来源= 堆栈跟踪:
答案 0 :(得分:0)
您的web.config缺少名为“ Raw_DataEntities”的连接字符串,将此指向您的数据库添加到它,它应该可以工作。
<connectionStrings>
<add name="Raw_DataEntities" connectionString="<your connection string" />
</connectionStrings>