我使用Web API获得了一个C#项目。我已经为我的控制器定义了我的前缀和路由,但在尝试访问" all"时,我一直收到错误。路线:
{
"message": "No HTTP resource was found that matches the request URI '.../api/InventoryOnHand/all'.",
"messageDetail": "No type was found that matches the controller named 'InventoryOnHand'."
}
这是我的控制人员:
[RoutePrefix("api/inventoryonhand")]
public class InventoryOnHandController : ApiController
{
public InventoryOnHandController(){}
[HttpGet]
[Route("all")]
[CacheOutput(ClientTimeSpan = 50, MustRevalidate = true)]
public IHttpActionResult GetAllInventoryOnHand()
{
// Do stuff
}
}
我的WebApiConfig不是问题(我认为),因为我们有其他路线工作正常,无法弄清楚为什么这个人是奇怪的人。我们在WebApiConfig中的路由:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
编辑添加WebApiConfig文件:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// require authenticated users in all controllers/action unless decoratd with "[AllowAnonymous]"
config.Filters.Add(new AuthorizeAttribute());
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
config.Services.Add(typeof(IExceptionLogger), new SerilogExceptionLogger());
ConfigureJsonHandling(config.Formatters.JsonFormatter);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Web API routes
config.MapHttpAttributeRoutes();
}
private static void ConfigureJsonHandling(JsonMediaTypeFormatter json)
{
//make our json camelCase and not include NULL or default values
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
json.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
json.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
}
编辑添加启动文件(为简洁起见缩短):
public void Configuration(IAppBuilder app)
{
LoggingConfig.ConfigureLogger();
HttpConfiguration httpConfiguration = new HttpConfiguration();
var container = IoC.Initialize();
httpConfiguration.DependencyResolver = new StructureMapResolver(container);
ConfigAuth(app);
WebApiConfig.Register(httpConfiguration);
GlobalConfiguration.Configure(WebApiConfig.Register);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(httpConfiguration);
Log.Logger.ForContext<Startup>().Information("======= Starting Owin Application ======");
}
答案 0 :(得分:1)
由于您使用的是属性,因此无法按惯例获取路由。在你的WebApiConfig中(你现在有了路由),你需要在config.MapHttpAttributeRoutes()中添加一行代码:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
对MapHttpAttributeRoutes扩展方法的调用将获取路由/路由前缀的属性,并为您的方法创建新路由。
答案 1 :(得分:0)
Attribute Routing in ASP.NET Web API 2
路由映射的顺序很重要。必须在基于约定的路由之前完成属性路由映射config.MapHttpAttributeRoutes()
,因为当框架匹配路由表中的路由时,第一个匹配将获胜。如果路线通过约定匹配,那么它将不会到达属性路线。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//..other code removed for brevity
// Attribute routing.
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
ApiController
[RoutePrefix("api/inventoryonhand")]
public class InventoryOnHandController : ApiController {
public InventoryOnHandController(){}
//GET api/inventoryonhand
[HttpGet]
[Route("")]
[CacheOutput(ClientTimeSpan = 50, MustRevalidate = true)]
public IHttpActionResult GetAllInventoryOnHand() {
// Do stuff
}
}
在初创公司中尝试此操作。
public void Configuration(IAppBuilder app)
{
LoggingConfig.ConfigureLogger();
HttpConfiguration httpConfiguration = GlobalConfiguration.Configuration;
GlobalConfiguration.Configure(WebApiConfig.Register);
var container = IoC.Initialize();
httpConfiguration.DependencyResolver = new StructureMapResolver(container);
ConfigAuth(app);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
Log.Logger.ForContext<Startup>().Information("======= Starting Owin Application ======");
}