随笔 - 435  文章 - 0  评论 - 110  阅读 - 62万 
用swagger访问https://localhost:44360/api/ads/1, 得到的结果是 TypeError: Failed to fetch。
一开始以为是后端代码问题,检查了好久,才发现是AdBlockPlus 把它当成广告过滤了。



 
 

I have the following routing, which works on invokation /api/demo/info/34.

[Route("api/demo")]
public class Demo : Controller
{
    [HttpGet("Info/{x}")]
    public JsonResult GetInfos(string x) { ... }
}

Now, I'd like to pass a query string to select the ID, like so: /api/demo/info?x=34. How should I rephrase the attribute for that?

When I tried entering [HttpGet("Info?x={x}")], the error message said that the question mark isn't valid there. I want to resolve it through the attributive approach and routing from the default mapping isn't an option.

----------------------------------------------

All you should need to do is to declare your attribute as:

[HttpGet("Info")]

while keeping the signature of the method as GetInfos(string x). In a GET route, WebAPI picks up all the parameters from the signature, and those which aren't present in the route can be passed as query string arguments as long as the name in the query string matches the name of the parameter.

posted on   Gu  阅读(25276)  评论(6编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2012-09-24 dom4j 如何保存&而不转义成&
点击右上角即可分享
微信分享提示