1.按照微軟官方文檔,若是要使用AttributeRoutes,須要在APP_START裏的WebApiConfig.cs的Register方法中添加一行:config.MapHttpAttributeRoutes();web
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);api
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();app
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
可是我加了以後死命報錯,劃小紅線,嚶嚶嚶
後來查到解決方法,是由於屬性路由是WebAPI 2中才加的,須要將VS升級到WebAPI 2。在菜單欄Tools-->NuGet package manager--->package manager console,輸入以下命令:asp.net
Install-Package Microsoft.AspNet.WebApi.WebHostide
運行完畢,重啓VS,小紅線是否是神奇的消失啦,啦啦啦.net
2.升級以後會遇到一些問題,好比後來寫代碼運行的時候,又出問題了,報錯信息以下:code
'CommonServices.Areas.HelpPage.XmlDocumentationProvider' does not implement interface member orm
解決方法,仍然是調出package manager console,運行以下命令:blog
install-package Microsoft.AspNet.WebApi.HelpPageci