【C#】解決MVC WEB API執行,出現404找不到資源

在MVC專案中新加入WEB API功能,在Controllers加入控制器"具有讀取/寫入 Web Api 2"
第一次加入後假設加入"Customer"(範例參考),會跳出一個txt檔。
專案中的Global.asax.cs檔案需做調整,才能正常啟用ASP.NET Web API

1.新增下列命名空間參考︰
  using System.Web.Http;
  using System.Web.Routing;

2.Application_Start()方法,加入下方標示藍色字體的方法且位置要在AreaRegistration.RegisterAllAreas()之後,RouteConfig.RegisterRoutes(RouteTable.Routes)之前︰
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);   // web api擺放位置
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}  

執行畫面(調整前)︰



執行畫面(調整後)︰

留言

這個網誌中的熱門文章

【ASP】日期轉換(西元<-->民國)

【VB】使用NPOI元件來匯出Excel--DataTableToExcel

【SQL】符號切割字串變成多欄