System.Web.Http下ApiController类

博客主要提及了属性和方法相关内容,但未给出具体信息。属性和方法是信息技术中常见概念,在编程等领域有重要应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

属性

名称描述
ActionContext获取ActionContext
Context获取http上下文
ControllerContext获取或设置ControllerContext
MetadataProvider获取IModelMetadataProvider
ModelState在模型绑定过程之后获取模型状态。 在模型绑定发生之前,此ModelState将为空。
ObjectValidator获取或设置IObjectModelValidator
Request获取或设置HTTP请求消息.
Url获取用于生成其他API的URL的工厂
User获取或设置与此请求关联的当前主体

方法

名称描述
BadRequest()Creates a BadRequestResult (400 Bad Request).
BadRequest(ModelStateDictionary)Creates an InvalidModelStateResult (400 Bad Request) with the specified model state.
BadRequest(String)Creates a BadRequestErrorMessageResult (400 Bad Request) with the specified error message.
Conflict()Creates a ConflictResult (409 Conflict).
Content<T>(HttpStatusCode, T)Creates a NegotiatedContentResult with the specified values.
Created(String, Object)Creates a CreatedResult (201 Created) with the specified values.
Created(Uri, Object)Creates a CreatedResult (201 Created) with the specified values.
CreatedAtRoute(String, Object, Object)Creates a CreatedAtRouteResult (201 Created) with the specified values.
Dispose()
Dispose(Boolean)
InternalServerError()Creates an InternalServerErrorResult (500 Internal Server Error).
InternalServerError(Exception)Creates an ExceptionResult (500 Internal Server Error) with the specified exception.
Json<T>(T, JsonSerializerSettings, Encoding)Creates an JsonResult (200 OK) with the specified values.
Json<T>(T, JsonSerializerSettings)Creates an JsonResult (200 OK) with the specified values.
Json<T>(T)Creates an JsonResult (200 OK) with the specified value.
NotFound()Creates an NotFoundResult (404 Not Found).
Ok()Creates an OkResult (200 OK).
Ok<T>(T)Creates an OkObjectResult (200 OK) with the specified values.
Redirect(Uri)Creates a RedirectResult (302 Found) with the specified value.
Redirect(String)Creates a RedirectResult (302 Found) with the specified value.
RedirectToRoute(String, Object)Creates a RedirectToRouteResult (302 Found) with the specified values.
ResponseMessage(HttpResponseMessage)Creates a ResponseMessageResult with the specified response.
StatusCode(HttpStatusCode)Creates a StatusCodeResult with the specified status code.
Validate<TEntity>(TEntity)Validates the given entity and adds the validation errors to the ModelState under an empty prefix.
Validate<TEntity>(TEntity, String)Validates the given entity and adds the validation errors to the ModelState.
<think>好的,我现在需要回答用户关于在C#中使用`System.Web.Http`的`ApiController`默认路由的问题。首先,用户之前问过ASP.NET Core中的默认路由,现在转向传统的ASP.NET Web API,所以我要确保区分清楚这两个框架。 首先,确定用户提到的包是`System.Web.Http`,这属于ASP.NET Web API(传统版,不是Core)。用户想知道继承`ApiController`的控制器默认路径是什么。我需要回忆或查找传统Web API的路由配置方式。 传统ASP.NET Web API通常使用基于约定的路由,即在`WebApiConfig.cs`文件中通过`MapHttpRoute`方法配置默认路由。默认路由模板是`"api/{controller}/{id}"`,其中`{action}`通常不在模板中,因为Web API默认基于HTTP方法选择动作,而不是通过URL中的动作名。 接下来,需要举例说明默认路径,例如`api/Products`对应到控制器的`Get`方法。同时,如果有多个Get方法,需要指出可能出现的歧义以及如何解决,比如使用属性路由或修改方法名。 还要注意,用户可能会混淆传统Web API和Core的区别,所以需要明确说明这是针对传统ASP.NET的情况,并建议使用属性路由来更清晰地定义路径,避免歧义。 最后,确保回答结构清晰,分点说明配置位置、默认模板、示例以及注意事项,帮助用户全面理解。</think>在传统 ASP.NET Web API(使用 `System.Web.Http` 命名空间)中,继承 `ApiController` 的控制器默认路由路径由 **`WebApiConfig.cs` 中的全局路由配置**决定,典型默认路径为: ```csharp // WebApiConfig.cs 中的默认配置 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", // ← 这是关键模板 defaults: new { id = RouteParameter.Optional } ); ``` ### 具体规则说明 1. **路径组成** - 默认路径格式:`/api/控制器名称/{可选参数}` - 例如:`ProductsController` 的默认路径为 `/api/products` 2. **方法匹配规则** - 根据 **HTTP 方法型**(GET/POST 等)自动匹配控制器中的方法 - 例如: ```csharp public class ProductsController : ApiController { // GET 请求默认匹配到 /api/products public IHttpActionResult Get() { ... } // GET 请求匹配 /api/products/5 public IHttpActionResult Get(int id) { ... } } ``` 3. **特殊场景** - 若存在多个同名 HTTP 方法(如多个 Get 方法),需通过 `[Route]` 属性或参数区分 - 若使用 `[RoutePrefix]` 或 `[Route]` 属性路由,会覆盖全局默认路由 ### 完整示例 ```csharp // WebApiConfig.cs public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); // 启用属性路由 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } // ProductsController.cs public class ProductsController : ApiController { // 默认路径:GET /api/products public IHttpActionResult GetAll() { ... } // 默认路径:GET /api/products/5 public IHttpActionResult GetById(int id) { ... } } ``` ### 关键区别(与传统 MVC 对比) || Web API (System.Web.Http) | MVC (System.Web.Mvc) | |---|---|---| | **基** | ApiController | Controller | | **路由默认前缀** | `/api` | 无 | | **方法匹配依据** | HTTP 动词 | URL 中的 action 名称 | | **默认路由模板** | `api/{controller}/{id}` | `{controller}/{action}/{id}` | 建议:在新项目中优先使用 ASP.NET Core,传统 Web API 已逐步被取代 ❗
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值