简体   繁体   English

为什么 nameof 不适用于 CreationAtAction 返回语句

[英]Why nameof doesn't work with the CreationAtAction return statement

I am developing a Web API using .NET Core ,when I send a post or an update to my web api, I am returning the resource.我正在使用.NET Core开发 Web API,当我向我的 Web API 发送帖子或更新时,我正在返回资源。 For that, at the end of each method, I am using CreatedAtAction method, where the first parameter is the action name.为此,在每个方法的末尾,我使用CreatedAtAction方法,其中第一个参数是操作名称。

When I am using a hard coded action name, CreatedAtAction("GetBook", [...] for example, it works. But when I am using CreatedAtRoute(nameof(GetBook), [...] I got an error saying that no routes matches. Is there a particulary reason for that ?例如,当我使用硬编码的动作名称CreatedAtAction("GetBook", [...] ,它可以工作。但是当我使用CreatedAtRoute(nameof(GetBook), [...]我收到了一个错误提示没有路线匹配。有什么特别的原因吗?

Look like you've mixed up the CreatedAt*Action* and CreatedAt*Route* methods.看起来您已经混淆了CreatedAt*Action*CreatedAt*Route*方法。 nameof(GetBook) should definitely return "GetBook" . nameof(GetBook)绝对应该返回"GetBook"

请参阅此链接: https : //docs.microsoft.com/en-us/aspnet/core/web-api/? view = aspnetcore -2.2 它显示CreatedAtAction (nameof(GetBook), [...]) 应该可以工作。

The CreatedAtRoute is a helper method which inherits from ControllerBase . CreatedAtRoute是一个从 ControllerBase 继承的辅助方法。 What CreatedAtRoute() does is simply returning a new CreatedAtRouteResult(routeName, routeValues, value) . CreatedAtRoute()所做的只是返回一个新的 CreatedAtRouteResult(routeName, routeValues, value) There is no dark magic at all :根本没有黑魔法:

/// <summary>
/// Creates a <see cref="CreatedAtRouteResult"/> object that produces a <see cref="StatusCodes.Status201Created"/> response.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The content value to format in the entity body.</param>
/// <returns>The created <see cref="CreatedAtRouteResult"/> for the response.</returns>
[NonAction]
public virtual CreatedAtRouteResult CreatedAtRoute(string routeName, object routeValues, object value)
=> new CreatedAtRouteResult(routeName, routeValues, value);

The CreatedAtRoute method is often used within a ApiController , and the third parameter will be used as the content of response . CreatedAtRoute方法经常在 ApiController 中使用,第三个参数将用作 response 的内容。 Let's say an action method :让我们说一个动作方法:

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    
    [HttpGet("{id}", Name = "HelloDetails")]
    public IActionResult GetDetail(int id)
    {
        var result = new MyItem() {
            Id=id,
            ItemName="sherry"
        }; 
        
        return new JsonResult(result);
    }

    [HttpPost]
    public async Task<IActionResult> Create([FromBody] MyItem item)
    {
        if (item == null)
        {
            return BadRequest();
        }
        
        return CreatedAtRoute("HelloDetails", new { id = item.Id }, item);
    }

Use Postman to test ,the result is a 201 response.使用 Postman 测试,结果是 201 响应。 The response's body contains details of the entity we created and the response's Location header contains a URI to the entity.响应的正文包含我们创建的实体的详细信息,响应的 Location 标头包含实体的 URI。

在此处输入图片说明

在此处输入图片说明

You could also have a look at this link你也可以看看这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM