C# 我不能在C中使用System.Net.Http.HttpMethod的属性作为开关表达式的参数

C# 我不能在C中使用System.Net.Http.HttpMethod的属性作为开关表达式的参数,c#,switch-statement,C#,Switch Statement,由于某种原因,我遇到了无法在switch语句中使用System.Net.Http.HttpMethod属性的问题。有趣的是,我可以在相同的方法中正常使用它,就在开关表达式之外。看看我的方法: private ObjectResult DeterminePositiveResponseType响应 { Console.WriteLineHttpMethod.Get.ToString; 返回HttpContext.Request.Method开关 { HttpMethod.Get.ToString=

由于某种原因,我遇到了无法在switch语句中使用System.Net.Http.HttpMethod属性的问题。有趣的是,我可以在相同的方法中正常使用它,就在开关表达式之外。看看我的方法:

private ObjectResult DeterminePositiveResponseType响应 { Console.WriteLineHttpMethod.Get.ToString; 返回HttpContext.Request.Method开关 { HttpMethod.Get.ToString=>Okresponse, HttpMethod.Post.ToString=>已创建,响应 }; } 目标是根据API的请求类型返回适当的ObjectResult

HttpMethod.Get.ToString应该只返回字符串Get,它在Console.WriteLine方法中返回。switch语句中的同一段代码给出错误:

类型“HttpMethod”中不存在类型名称“Get”

我不知道为什么会发生这种情况,如果有人能给我解释一下,我将不胜感激。谢谢。

切换运算符中的case值应该是常量,这意味着case中的任何ToString都不会工作,因为它将在运行时执行。您需要明确定义您的值:

 return HttpContext.Request.HttpMethod switch
 {
     "GET" => Ok(response),
 }
switch操作符中的case值应该是常量,这意味着case中的任何ToString都不会工作,因为它将在运行时执行。您需要明确定义您的值:

 return HttpContext.Request.HttpMethod switch
 {
     "GET" => Ok(response),
 }
您可以这样做:

        private ObjectResult DeterminePositiveResponseType<T>(T response)
    {
        var methodType = HttpContext.Request.Method;

        switch (methodType)
        {
            case string m when HttpMethod.Get.ToString() == methodType:
                return Ok(response);
            case string m when HttpMethod.Post.ToString() == methodType:
                return Created("",response);
            default:
                return Ok(response);
        }
    }
您可以这样做:

        private ObjectResult DeterminePositiveResponseType<T>(T response)
    {
        var methodType = HttpContext.Request.Method;

        switch (methodType)
        {
            case string m when HttpMethod.Get.ToString() == methodType:
                return Ok(response);
            case string m when HttpMethod.Post.ToString() == methodType:
                return Created("",response);
            default:
                return Ok(response);
        }
    }

使用新的开关语法可以通过以下方式实现:

private ObjectResult DeterminePositiveResponseType<T>(T response)
{
    Console.WriteLine(HttpMethod.Get.ToString());
    return HttpContext.Request.Method switch
    {
        string s when s == HttpMethod.Get.ToString() => Ok(response),
        string s when s == HttpMethod.Post.ToString() => Created("", response)
        _ => Ok(response), //this line to prevent throwing of SwitchExpressionException
    };
}

使用新的开关语法可以通过以下方式实现:

private ObjectResult DeterminePositiveResponseType<T>(T response)
{
    Console.WriteLine(HttpMethod.Get.ToString());
    return HttpContext.Request.Method switch
    {
        string s when s == HttpMethod.Get.ToString() => Ok(response),
        string s when s == HttpMethod.Post.ToString() => Created("", response)
        _ => Ok(response), //this line to prevent throwing of SwitchExpressionException
    };
}

我很确定你看到了什么,因为HttpMethod.Get不是常量值,它是一个属性,我认为该开关需要常量值。请不要发布代码的图像。另请参见:@MiB_Coder:代码块中的问题代码,以及错误消息。据推测,还包括这些图像,以说明VisualStudio的代码分析检测到的错误。这对我来说似乎是一个可以接受的嵌入图像的用法。我很确定你看到的是什么,因为HttpMethod.Get不是常量值,它是一个属性,我认为开关需要常量值。请不要发布代码的图像。另请参见:@MiB_Coder:代码块中的问题代码,以及错误消息。据推测,还包括这些图像,以说明VisualStudio的代码分析检测到的错误。对我来说,这似乎是一个可以接受的嵌入图像的用法。这种方法很好,因为它不需要将属性名硬编码为字符串。在这里,当谈到诸如HttpMethod之类的知名api时,这可能不是一个问题,这些api受外部标准的约束,不太可能很快改变。但作为一般规则,这有助于在未来版本中接口发生更改时保持引用完整性。这种方法很好,因为它不需要将属性名硬编码为字符串。在这里,当谈到诸如HttpMethod之类的知名api时,这可能不是一个问题,这些api受外部标准的约束,不太可能很快改变。但作为一般规则,这有助于在未来版本中接口发生更改时保持引用完整性。