asp.net-web-api - FormatAttribute 需要正斜杠来执行不带参数的操作
问题描述
我有一个没有参数的 GET 方法,并希望在下面工作
/api/books.xml
然而,这适用于正斜杠
/api/books/.xml
[Route("api/[controller]")]
[ApiController]
public class BooksController : ControllerBase
{
[HttpGet]
[Route(".{format}")]
[FormatFilter]
public ActionResult<List<Book>> Get()
{
return bookService.Get();
}
}
我尝试过的可能解决方案是
没有 {id} 的注释
[Route("[controller]/[action].{format}")] // no slash between [action] and .{format}
在 Startup.cs 中添加一个不带 {id} 的默认路由,这样如果 id 参数没有像在这个问题中那样传递,那么路由不应该期望 {action} 之后有斜线。
app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller}/{action=Index}"); });
解决方案
根据控制器上当前定义的路由,您描述的是设计使然。
考虑更改路由以匹配所需的 URL 格式
[ApiController]
public class BooksController : ControllerBase {
[HttpGet]
[Route("api/[controller].{format}")] //<--- GET api/books.xml
[FormatFilter]
public ActionResult<List<Book>> Get() {
return bookService.Get();
}
}
推荐阅读
- matlab - 将 np.einsum() 转换为 MATLAB
- flutter - 多图像上传导致应用程序崩溃说打开的文件太多
- api - Twitter Account Activity API 的 webhook 交付不一致
- scala - 展平 DataFrame 中的嵌套架构,获取 AnalysisException:无法解析列名
- kubernetes - 集群没有稳定的 controlPlaneEndpoint 地址
- python - Python中的修剪平均值
- python - 如何将 n 列切换为 ar 行 pandas 数据帧的行(最终数据帧中的 n*r 行)?
- node.js - 如何将项目依赖项与 ts-node 一起使用
- python - 如何在 python 中处理不平衡的数据集和异常值?
- python-3.x - 是否有任何 API 端点来检查 DAG 是否为 PAUSESD / UNPAUSED?