我有一个通用控制器,它有几个派生的控制器类.但我无法弄清楚如何处理HttpGet的路由名称,因为它需要不变.
[HttpGet("{id}", Name ="should not hard coded here for derived class")]
public virtual async Task<IActionResult> Get(int id)
Run Code Online (Sandbox Code Playgroud)
我需要路由名称,因为在我的HttpPost函数中我想返回需要HttpGet的路由名称的 CreatedAtRoute()
路由名称不能进行硬编码,因为所有派生类都需要具有不同的路由名称.
这是基本控制器
public abstract class BaseController<TEntity, TContext> : Controller where TEntity : BaseOptionType, new() where TContext : DbContext
{
private readonly IGenericRepository<TEntity, TContext> _repository;
private readonly ILogger<BaseGenericOptionTypesController<TEntity, TContext>> _logger;
public BaseController(IGenericRepository<TEntity, TContext> repository, ILogger<BaseController<TEntity, TContext>> logger)
{
_repository = repository;
_logger = logger;
}
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[HttpGet("{id}", Name = "should not hard code here for derived class")]
public …Run Code Online (Sandbox Code Playgroud) c# asp.net-core-mvc asp.net-core-webapi asp.net-core-routing
我正在使用 asp.net 5
我有两个嵌套的模型类,两个内部类都被命名Command
public class EditModel
{
public class Command
{
public int Id { get; set; }
public string Info { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class CreateModel
{
public class Command
{
public int Id { get; set; }
public string Info { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器类中有两个方法
[HttpPost]
public IActionResult PutData(CreateModel.Command model)
{
return Ok();
}
[HttpPut]
public IActionResult PostData(EditModel.Command model)
{
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
由于对于 Put 和 Post 的查询我都使用嵌套类 name Command …