在.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
ApiController要同时出现,可以在Controller上加ApiController,然后在Action上Route特性
去掉 [ApiController]
加了之后也是继承自ControllerBase,MVC的控制器也是间接继承自ControllerBase,那不加和MVC的控制器还有区别吗?
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()
mark!