C# 如何在ASP.NET Core 2中更改CreateDataAction的路由格式?

C# 如何在ASP.NET Core 2中更改CreateDataAction的路由格式?,c#,asp.net-core-2.0,asp.net-core-webapi,C#,Asp.net Core 2.0,Asp.net Core Webapi,在EF Core 2中,CreateDataAction将参数作为api/sample?id=1追加。如何将其配置为返回api/sample/1 [ApiController] [Authorize] [Route("api/[controller]")] public class MyController : ControllerBase { [HttpGet("{id}")] public async Task<I

在EF Core 2中,CreateDataAction将参数作为api/sample?id=1追加。如何将其配置为返回api/sample/1

    [ApiController]
    [Authorize]
    [Route("api/[controller]")]
    public class MyController : ControllerBase
    {
        [HttpGet("{id}")]
        public async Task<IActionResult> Get(int id)
        {
            var entity = await service.GetAsync(id);
            if (entity == null)
                return NotFound(entity);
            return Ok(entity);
        }

        [HttpPut]
        // Other codes are omitted for brevity
        public async Task<IActionResult> Create([FromBody] Entity entity)
        {
            await service.AddAsync(entity);
            await service.SaveAsync();
            return CreatedAtAction(action, new { id = entity.Id }, entity);  
        }    
    }
[ApiController]
[授权]
[路由(“api/[控制器]”)]
公共类MyController:ControllerBase
{
[HttpGet(“{id}”)]
公共异步任务Get(int-id)
{
var entity=await service.GetAsync(id);
if(实体==null)
返回未找到(实体);
返回Ok(实体);
}
[HttpPut]
//为简洁起见,省略了其他代码
公共异步任务创建([FromBody]实体)
{
wait service.AddAsync(实体);
wait service.SaveAsync();
返回CreateDataAction(操作,新的{id=entity.id},entity);
}    
}

这是因为操作的路由模板

很可能是因为默认的基于约定的路由

这可以通过将属性路由放置在具有预期路由模板的所需操作上来解决

[Route(api/[controller])]
public class SampleController : Controller {
    //GET api/sample/1
    [HttpGet("{id}")]
    public IActionResult Get(int id) {
        //...
    }

    //POST api/sample
    [HttpPost]
    public IActionResult Post(Sample model) {
        //...

        return CreatedAtAction(nameof(Get), new { id = model.Id }, model);
    }
}

以及确保在创建结果时使用正确的操作名称。上面的示例使用了
Get
操作,因此在为创建的响应中的
位置
生成URL时将使用其路由模板。

这是因为该操作的路由模板

很可能是因为默认的基于约定的路由

这可以通过将属性路由放置在具有预期路由模板的所需操作上来解决

[Route(api/[controller])]
public class SampleController : Controller {
    //GET api/sample/1
    [HttpGet("{id}")]
    public IActionResult Get(int id) {
        //...
    }

    //POST api/sample
    [HttpPost]
    public IActionResult Post(Sample model) {
        //...

        return CreatedAtAction(nameof(Get), new { id = model.Id }, model);
    }
}

以及确保在创建结果时使用正确的操作名称。上面的示例使用了
Get
操作,因此在为创建的响应中的
位置
生成URL时将使用其路由模板。

My API已经在使用API/sample/product/1模板,几乎所有操作都默认使用该模板。唯一返回不同路由的是CreateDataAction。@JeonsoftFaceBundy然后需要显示导致问题的代码,以便提供更准确的答案。更新原始问题。我更新了上面的描述。我想这和你的例子很相似。事实上,我已经解决了这个问题。我一直使用nameOf(Create)作为参数。应该是nameOf(Get)我的API已经在使用API/sample/product/1模板,并且几乎所有操作都默认使用该模板。唯一返回不同路由的是CreateDataAction。@JeonsoftFaceBundy然后需要显示导致问题的代码,以便提供更准确的答案。更新原始问题。我更新了上面的描述。我想这和你的例子很相似。事实上,我已经解决了这个问题。我一直使用nameOf(Create)作为参数。它应该是nameOf(Get)事实上,我已经解决了这个问题。我一直使用nameOf(Create)作为参数。它应该是name of(Get)。谢谢事实上,我已经解决了这个问题。我一直使用nameOf(Create)作为参数。它应该是name of(Get)。谢谢