如何在mvc项目中使用apiController

 

文章地址:How do you route from an MVC project to an MVC ApiController in another project?

 

文章地址:How to Use MVC Controller and WebAPI Controller in same project

You need to register the routing for web api BEFORE registering the routing for MVC, so basically your App_Start()function should look like this:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);//WEB API 1st
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);//MVC 2nd
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

 

需要知道的知识点:

1,mvc项目你需要在Global.asax.cs文件Application_Start方法中注册路由;

2,mvc路由和api路由是两种不一样的路由;

mvc路由一般在App_Start文件夹下RouteConfig.cs文件中;

api路由在App_Start文件夹下WebApiConfig.cs文件中;

 

文章:How do you route from an MVC project to an MVC ApiController in another project?

很好的介绍了如何在mvc项目中使用apiController,即需要配置api路由。

api路由类型是:MapHttpRoute ,mvc路由类型是:MapRoute 。

但是最后两种路由都在集合:Routes中。api路由需要配置在mvc路由之前。

同时路由还有命名空间限制,需要注意!

 

posted on   荆棘人  阅读(786)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2017-01-10 vs调试时报503错误

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示