新WebApi项目没有API的默认路由(但仍然可以工作)
本文关键字:工作 路由 项目 WebApi API 默认 | 更新日期: 2023-09-27 18:04:15
我已经创建了一个新的WebAPI MVC项目,API控制器有路径http://localhost:1234/api
,它们从这个路由工作,但是RegisterRoutes
类不包含默认路由,它包含以下内容:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
API的路由在哪里?
欢呼戴夫Visual Studio项目模板创建了一个默认路由,如下所示:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
您可以在WebApiConfig.cs
文件中找到它,该文件位于App_Start
目录
它位于另一个类中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace HelloWorldApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
// To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
// For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
//config.EnableQuerySupport();
// To disable tracing in your application, please comment out or remove the following line of code
// For more information, refer to: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
}
}