为什么下面的设置不起作用,我已经开了几个小时了。它抛出404。
到目前为止我做了什么:
我错过了什么?我还应该检查什么?
它是如何工作的,或者我想要工作的。 WebApi控制器只获取businesslogic作为参数,Autofac应该处理它,而BusinessLogic库获取其他树组件(Mapper,SoapClient,Repository)。它应该工作,或者至少我在我的其他应用程序中有类似的设置,但是那个也有Owin设置。
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
var builder = new ContainerBuilder();
var config = GlobalConfiguration.Configuration;
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config);
builder.RegisterType<Repository.Repository.Repository>()
.As<IRepository.IRepository.IRepository>();
builder.RegisterType<BusinessLogic.BusinessLogic.BusinessLogic.BusinessLogic>()
.As<IBusinessLogic>();
builder.RegisterType<SoapClient>()
.As<ISoapClient>();
builder.RegisterType<Mapper.Mapper.Mapper.Mapper>()
.As<IMapper>();
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
//GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
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 }
);
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
}
[RoutePrefix("PipelineWorkflow")]
public class PipelineWorkflowController : ApiController
{
#region Private variables
private IBusinessLogic _businessLogic;
#endregion
#region Constructors
public PipelineWorkflowController(IBusinessLogic businessLogic)
{
_businessLogic = businessLogic;
}
#endregion
[HttpGet]
[Route("GetPipelineWorkflows")]
public string GetPipelineWorkflows()
{
return "asd";
}
}
已安装的Nuget包
PM> Get-Package -project GoNoGo.Services.webapi | Format-Table -Autosize
Id Versions ProjectName
-- -------- -----------
Autofac {3.5.2} GoNoGo.Services.WebApi
Autofac.WebApi2 {3.4.0} GoNoGo.Services.WebApi
EntityFramework {6.1.3} GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi {5.2.3} GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi.Client {5.2.3} GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi.Core {5.2.3} GoNoGo.Services.WebApi
Microsoft.AspNet.WebApi.WebHost {5.2.3} GoNoGo.Services.WebApi
Newtonsoft.Json {8.0.2} GoNoGo.Services.WebApi
提前感谢您的帮助!
更新:
答案 0 :(得分:0)
如何取消注释?
//GlobalConfiguration.Configure(WebApiConfig.Register);
没有该部分就没有路由激活,也没有经典的属性路由,所以我猜404是正确的答案。我怀疑问题是Autofac;顺便说一句,容器问题几乎每次都解决为500,而不是404。
希望有所帮助:)