我看到这个错误被问了很多,但到目前为止还没有运气。我有一个.Net 4(VS2010)网站SITE项目。我有一个Web API项目作为一个单独的项目,在同一个解决方案中。我可以从本地机器上的网页调用Web API,没问题。我将整个事情推向生产,并且web api调用失败:
{"$id":"1","Message":"No HTTP resource was found that matches the request URI 'http://khdecisiontool.ecitystage.net/api/slider/69'.","MessageDetail":"No type was found that matches the controller named 'slider'."}
要注册我在Global.asax中执行此操作的所有内容:
void Application_Start(object sender, EventArgs e)
{
//This enables api controllers in a separate class library
GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new AssembliesResolver());
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
我的API路线是这样的:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
).RouteHandler = new ECityHttpControllerRouteHandler();
这是在我的网络表格路线后放置的。
修改
这是控制器:
/// <summary>
/// Api controller for sliders
/// </summary>
[Authorize]
public class SliderController : ApiController
{
SliderService context = new SliderService();
// GET api/slider
/// <summary>
/// Get a list of all sliders as slider dtos
/// </summary>
/// <returns></returns>
public IEnumerable<SliderDto> Get()
{
var sliderList = new List<SliderDto>();
var sliders = context.GetSliders();
foreach (var slider in sliders)
{
var sliderDto = new SliderDto
{
ID = slider.ID,
PersonID = slider.PersonID,
ContentPageID = slider.ContentPageID,
StartValue = slider.StartValue,
EndValue = slider.EndValue,
Date = slider.Date.ToLongDateString() + slider.Date.ToLongTimeString()
};
sliderList.Add(sliderDto);
}
return sliderList.ToList();
}
// GET api/slider/5
/// <summary>
/// Get a single slider as sliderDto
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public SliderDto Get(int id)
{
var slider = context.GetSlider(id);
if (slider != null)
{
var sliderDto = new SliderDto
{
ID = slider.ID,
PersonID = slider.PersonID,
ContentPageID = slider.ContentPageID,
StartValue = slider.StartValue,
EndValue = slider.EndValue,
Date = slider.Date.ToLongDateString() + slider.Date.ToLongTimeString()
};
return sliderDto;
}
return new SliderDto { StartValue = 0, EndValue = 0 };
}
// POST api/slider
/// <summary>
/// Insert a slider
/// </summary>
/// <param name="sliderDto">The slider Dto</param>
/// <returns></returns>
public HttpResponseMessage Post(SliderDto sliderDto)
{
var slider = new Slider
{
ID = sliderDto.ID,
PersonID = sliderDto.PersonID,
ContentPageID = sliderDto.ContentPageID,
StartValue = sliderDto.StartValue,
EndValue = sliderDto.EndValue,
Date = DateTime.Now
};
var id = context.AddSlider(slider);
var item = context.GetSlider(id);
var response = Request.CreateResponse<Slider>(HttpStatusCode.Created, item);
string uri = Url.Link("Default", new { id = id });
response.Headers.Location = new Uri(uri);
return response;
}
// PUT api/slider/5
/// <summary>
/// Update a slider using the SliderDto
/// </summary>
/// <param name="id"></param>
/// <param name="sliderDto"></param>
public void Put(int id, SliderDto sliderDto)
{
try
{
var data = context.GetSlider(id);
data.ID = sliderDto.ID;
data.PersonID = sliderDto.PersonID;
Nullable<int> contentId = null;
if (sliderDto.ContentPageID.HasValue && sliderDto.ContentPageID.Value != 0)
contentId = sliderDto.ContentPageID.Value;
data.ContentPageID = contentId;
data.StartValue = sliderDto.StartValue;
data.EndValue = sliderDto.EndValue;
data.Date = DateTime.Now;
context.UpdateSlider(data);
}
catch (Exception ex)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
// DELETE api/slider/5
/// <summary>
/// Delete a slider
/// </summary>
/// <param name="id"></param>
public void Delete(int id)
{
var item = context.GetSlider(id);
if (item == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
context.DeleteSlider(item);
}
}
编辑2
这是装配解析器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Reflection;
/// <summary>
/// Summary description for AssembliesResolver
/// </summary>
public class AssembliesResolver : System.Web.Http.Dispatcher.DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
ICollection<Assembly> baseAssemblies = base.GetAssemblies();
List<Assembly> assemblies = new List<Assembly>(baseAssemblies);
var controllersAssembly = Assembly.Load("MyAssemblyName");
baseAssemblies.Add(controllersAssembly);
return assemblies;
}
}