1、直接在Global.asax中新增配置
如:
using MvcApplication4.App_Start; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; namespace MvcApplication4 { // 注意: 有關啟用 IIS6 或 IIS7 經典模式的說明, // 請訪問 http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { RouteTable.Routes.MapHttpRoute("WebApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional }); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); BundleTable.EnableOptimizations = false; } } }
2、在App_Start資料夾中新增WebAPIConfig.cs類,後在Global.asax檔案,在 Application_Start 方法中 初始化路由對映
WebAPIConfig.cs:
Global.asax
protected void Application_Start(object sender, EventArgs e) { //在應用程式啟動時註冊路由對映 WebAPIConfig.Register(GlobalConfiguration.Configuration); }