【Web API]无法添加AttributeRoutes的解决方案

本文介绍了在VS中使用AttributeRoutes时遇到的问题及解决办法。按照微软文档添加代码后报错,原因是属性路由为WebAPI 2新增,需将VS升级到WebAPI 2。升级后运行代码又出现问题,通过在package manager console运行相应命令可解决。

1.按照微软官方文档,如果要使用AttributeRoutes,需要在APP_START里的WebApiConfig.cs的Register方法中添加一行:config.MapHttpAttributeRoutes();

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 }
);

// 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();

// 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,输入如下命令:

Install-Package Microsoft.AspNet.WebApi.WebHost

运行完毕,重启VS,小红线是不是神奇的消失啦,啦啦啦

 

2.升级之后会遇到一些问题,比如后来写代码运行的时候,又出问题了,报错信息如下:

'CommonServices.Areas.HelpPage.XmlDocumentationProvider' does not implement interface member 

解决方法,仍然是调出package manager console,运行如下命令:

install-package Microsoft.AspNet.WebApi.HelpPage

问题解决啦
---------------------
作者:missyr
来源:优快云
原文:https://blog.youkuaiyun.com/qingtian12138/article/details/73857018
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/Jeely/p/10956747.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值