首页 新闻 赞助 找找看

.net core2.2版本上的webapi的控制器都需要加Route特性吗?

0
悬赏园豆:5 [已解决问题] 解决于 2019-09-11 18:01

在.net framework框架的webapi,可以通过全局路由注册来实现和MVC的访问方式一样的url中带Controller和Action,现在遇到问题是.net core下的不加Route会报错

System.InvalidOperationException: Action 'HouAspNetCore2_2.Controllers.ValuesController.Get (HouAspNetCore2_2)' does not have an attribute route. Action methods on controllers annotated with ApiControllerAttribute must be attribute routed

猴哥aiyy的主页 猴哥aiyy | 初学一级 | 园豆:3
提问于:2019-09-08 16:55
< >
分享
最佳答案
0

ApiController要同时出现,可以在Controller上加ApiController,然后在Action上Route特性

猴哥aiyy | 初学一级 |园豆:3 | 2019-09-11 18:00
其他回答(4)
0

去掉 [ApiController]

收获园豆:2
dudu | 园豆:31094 (高人七级) | 2019-09-08 17:41

加了之后也是继承自ControllerBase,MVC的控制器也是间接继承自ControllerBase,那不加和MVC的控制器还有区别吗?

支持(0) 反对(0) 猴哥aiyy | 园豆:3 (初学一级) | 2019-09-08 17:48
0
收获园豆:2
@索卡 | 园豆:140 (初学一级) | 2019-09-09 17:47
1

Action methods on controllers annotated with ApiControllerAttribute must be attribute routed.
是的,API方式是这样的
如果不用webapi,restful风格
-----------------------------
路由:endpoints.MapControllers();
-----------------------------
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
-----------------------------
其实MVC模式也一样的,只是继承类不同;去掉了MV只用C。
startup.cs增加注册路由
endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");

public class Hello : Controller {
public string Index()

收获园豆:1
张朋举 | 园豆:1915 (小虾三级) | 2019-09-09 22:03
0

mark!

代码小王子来了 | 园豆:208 (菜鸟二级) | 2021-09-29 14:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册