使用John Papas的SPA教程访问SQL Server Compact DB。当我使用URL访问应用程序中的扬声器列表时,它会抛出404错误。我已经尝试通过再次浏览教程视频来解决过去一小时的问题,但我没有运气。
本教程让我通过以下URL访问:localhost:xxxxx / api / speakers。
因此,我的URL看起来像这样:localhost:12345 / api / speakers
Error message: {"message":"No HTTP resource was found that matches the request URI 'http://localhost:49357/api/speakers'.","messageDetail":"No type was found that matches the controller named 'speakers'."}
speakers.html位于App文件夹中。
这是我在App_Start下的WebApiConfig.cs:
using System.Web.Http;
using Newtonsoft.Json.Serialization;
namespace CodeCamper
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
Configure(config);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
private static void Configure(HttpConfiguration config)
{
config.Formatters.Remove(config.Formatters.XmlFormatter);
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
}
}}
这是教程提供的SpeakersController:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CodeCamper.Controllers
{
public class SpeakersController
{
// GET api/speakers
public IEnumerable<object> Get()
{
var ctx = new CodeCamperDbContext();
var query = ctx.Set<Session>()
.Select(session => session.Speaker)
.Distinct().Select(s =>
new
{
Id = s.Id,
FirstName = s.FirstName,
LastName = s.LastName,
ImageSource = s.ImageSource,
});
return query;
}
}
}
任何想法我做错了什么?提前谢谢。
答案 0 :(得分:0)
您必须从SpeakersController
ApiController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CodeCamper.Controllers
{
public class SpeakersController: System.Web.Http.ApiController // <-- here it is
{
// GET api/speakers
public IEnumerable<object> Get()
{
var ctx = new CodeCamperDbContext();
var query = ctx.Set<Session>()
.Select(session => session.Speaker)
.Distinct().Select(s =>
new
{
Id = s.Id,
FirstName = s.FirstName,
LastName = s.LastName,
ImageSource = s.ImageSource,
});
return query;
}
}
}
答案 1 :(得分:0)
从SpeakersController
ApiController
public class SpeakersController : System.Web.Http.ApiController
{
//your code
}
并添加
在HttpRoute("api/speakers")
// GET api/speakers