【问题标题】:Attribute Based Routing/Versioning in ASP.Net WEB API 2.0ASP.Net WEB API 2.0 中基于属性的路由/版本控制
【发布时间】:2020-10-03 14:10:41
【问题描述】:

我正在尝试在 Asp.Net Web API 中使用版本控制。 以下是项目的结构。

为了支持版本控制,我添加了 Microsoft.AspNet.WebApi.Versioning NuGet 包。 下面是WebApiConfig的sn-p代码:

    public static void Register(HttpConfiguration config)
    {
        var constraintResolver = new DefaultInlineConstraintResolver()
        {
            ConstraintMap =
            {
                ["apiVersion"] = typeof(ApiVersionRouteConstraint)
            }
        };
        config.MapHttpAttributeRoutes(constraintResolver);
        config.AddApiVersioning();
        // Web API configuration and services

        // Web API routes
        //config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

以下是控制器的代码:

[ApiVersion("1.0")]
[RoutePrefix("api/v{version:apiVersion}/employeemanagement")]
public class EmployeeManagementController : ApiController
{
    [Route("GetTest")]
    [HttpGet]
    public string GetTest()
    {
        return "Hello World";
    }

    [Route("GetTest2")]
    [HttpGet]
    public string GetTest2()
    {
        return "Another Hello World";
    }

    [Route("saveemployeedata")]
    [HttpPost]
    public async Task<GenericResponse<int>> SaveEmployeeData(EmployeeData employeeData, ApiVersion apiVersion)
    {
        //code goes here
    }

    [Route("updateemployeedata")]
    [HttpPost]
    public async Task<GenericResponse<int>> UpdateEmployeeData([FromBody]int id, ApiVersion apiVersion)
    {
        //code goes here            
    }
}

如果我在 UpdateEmployeeData 中使用 [FromBody],则会出现以下错误:

{
"Message": "The request is invalid.",
"MessageDetail": "The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Threading.Tasks.Task`1[AlphaTest.API.Models.ResponseModels.GenericResponse`1[System.Int32]] UpdateEmployeeData(Int32, Microsoft.Web.Http.ApiVersion)' in 'AlphaTest.API.Controllers.V1.EmployeeManagementController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
}

以下是 URL 和数据,我传递以生成上述错误: http://localhost:53963/api/v1.0/EmployeeManagement/updateemployeedata

如果我删除[FromBody],它会给我404 Not found error

请帮助我了解我在这里做错了什么,这导致了上述错误。

【问题讨论】:

    标签: c# asp.net-web-api asp.net-web-api2 asp.net-web-api-routing


    【解决方案1】:

    您可以使用包含名为Id 的属性的对象 作为动作UpdateEmployeeData参数,而不是直接使用int Id,例如:

    public class Request
    {
        public int Id { get; set; }
    }
    

    动作将是:

    [Route("updateemployeedata")]
    [HttpPost]
    public async Task<GenericResponse<int>> UpdateEmployeeData([FromBody]Request request, ApiVersion apiVersion)
    {
        //code goes here            
    }
    

    希望对您有所帮助。

    【讨论】:

    • 是的。你说得对。我们可以按照您提到的方式达到预期的效果。但是我遵循的方法有什么问题。
    • @VivekSharma 您的代码中的问题是您发送的对象的属性名为id,而不是整数。看看它是如何括在括号中的,并且值是如何设置为属性的。一个整数直接是body上的值,没有别的,尝试在JSON中序列化一个整数,你会得到的结果将是纯文本中的数字。
    • @VivekSharma 您可以使用app.quicktype.io/?l=csharp 从 Json 生成 c# 代码,如果您有复杂的 json,这将对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 2014-06-09
    • 2015-07-19
    相关资源
    最近更新 更多