C# Asp.net core 3.1将Url中的斜杠(/)替换为问题Marq(?)

C# Asp.net core 3.1将Url中的斜杠(/)替换为问题Marq(?),c#,asp.net-mvc,asp.net-core,asp.net-web-api,C#,Asp.net Mvc,Asp.net Core,Asp.net Web Api,在Asp.net core 3.1中,我们要配置路由 我们有一个行动 [HttpGet("GetDepart/{id}")] public async Task<ActionResult<Depart>> GetDepart(int id) { //Return a Department } [HttpGet(“GetDepart/{id}”)] 公共异步任务GetDepart(int-id) { //返回一个部门 } 如何配置路由以将斜杠

在Asp.net core 3.1中,我们要配置路由

我们有一个行动

[HttpGet("GetDepart/{id}")]
public async Task<ActionResult<Depart>> GetDepart(int id)
{
    //Return a Department
}
[HttpGet(“GetDepart/{id}”)]
公共异步任务GetDepart(int-id)
{
//返回一个部门
}
如何配置路由以将斜杠(/)替换为问号(?) 在Url中


因此,与其拥有http://localhost:44339/Departs/GetDepart/1,我们想要http://localhost:44339/Departs/GetDepart ?id=1

您可能正在寻找

像这样的事情会让你达到你想去的地方

[HttpGet(“GetDepart”)]
公共异步任务GetDeep([FromQuery]int id)
{
//返回一个部门
}

请注意
id
参数上的
[FromQuery]
属性。

HttpGetAttribute需要模板参数,但模板不能包含“?”字符,这是经过设计的。 所以,如果您想使用
?id=1
,您可以这样使用(但这种方式与route不同,当http://localhost:44339/Departs/GetDepart 它也可以转到操作:

[HttpGet(“GetDepart”)]

公共异步任务。

是否将/替换为?例如,使用
string.Replace(“/”,“?”)但这不是你要找的。也许这会有所帮助:@Jelle该链接与此问题无关。@PanagiotisKanavos这篇文章被一分为二,我应该马上发送第二部分:@Jelle第一篇文章是关于WebForms的,除了在单独的片段中,没有真正讨论查询字符串。它的大部分内容只是描述如何使用几个fields@Jelle,问题是关于Asp.net核心web api中的路由,我需要在Url中使用“?”而不是“/”,这需要在HttpGet动词中为Action配置,我使用属性[HttpGet(“GetDeep/{id}”)配置路由]但是我必须用斜杠(/)来放置URL,我如何替换[HttpGet(“GetDeep/{id}”)来接收“?”而不是“/”同意,但在我看来,GetDeep/{id}更好地用于RouteThaks,因为它可以工作,如果参数是一个模型,那么我必须使用HttpGet,或者我必须将其更改为HttpPost,因为模型不能在URL中发送。
[HttpGet("GetDepart")]

    public async Task<ActionResult<Depart>> GetDepart(int id)
    {
        //Return a Department
    }