C# Razor using 语句不一致地要求显式大括号

C# Razor using statements inconsistently require explicit curly brackets

提问人:bowserm 提问时间:5/31/2023 更新时间:5/31/2023 访问量:22

问:

我有一个 .NET Framework 4.7.2 Web 应用程序。我从我的一些视图和部分视图(但不是全部)收到以下黄屏错误:

Expected a "{" but found a "using".
Block statements must be enclosed in "{" and "}".
You cannot use single-statement control-flow statements in CSHTML pages...

Web 应用程序中的某些视图和部分视图使用需要释放的流。我意识到剃须刀不是处理流的理想场所。现在,我试图理解为什么 Razor 有时喜欢我的陈述,而不喜欢其他陈述。using

其中一个视图有这样的代码,效果很好:

@{
    string baseUrl = "redacted";
    string requestUrl = "redacted";
    ...
    ...

    #region Redacted Region Name
        try
        {
            HttpWebRequest httpWebRequest = WebRequest.Create(requestUrl) as HttpWebRequest;
            httpWebRequest.Method = "GET";

            using (HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse)
            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                var data = reader.ReadToEnd();
                ...
            }
        }
        catch (Exception ex)
        {
            ...
        }
    #endregion
}

但是,具有以下语句的单独视图将导致黄屏错误和 Visual Studio 抱怨语法错误:using

@{
    string baseUrl = "redacted";
    string requestUrl = "redacted";
    ...
    ...

    HttpWebRequest httpWebRequest = WebRequest.Create(requestUrl) as HttpWebRequest;
    httpWebRequest.Method = "GET";

    using (HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse)
    using (var stream = response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
        var data = reader.ReadToEnd();
        ...
    }
}

我知道我可以应用显式的大括号,我的问题将得到解决,如下面的代码片段所示。我想知道为什么我不能堆叠这些语句,并省略我的一些视图中的一些大括号。为什么它有时有效,但在 Razor 中其他无效?using

using (HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse)
{
    using (var stream = response.GetResponseStream())
    {
        using (var reader = new StreamReader(stream))
        {
            var data = reader.ReadToEnd();
            ...
        }
    }
}
C# Razor Razor using-statement using -指令

评论


答:

0赞 bowserm 5/31/2023 #1

问题似乎在于 Razor 如何决定我是使用“using 语句”还是“using 指令”(请参阅 https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using)

  • using 指令:帮助命名空间
  • using 语句:帮助处理垃圾回收器不会自动清理的对象

到目前为止,我已经找到了三种与 Razor 沟通的方法,我想将关键字用于“using 语句”using

  1. 使用大括号
  2. 将“using 语句”封装在函数或 html 帮助程序中
  3. 将语句括在using#region

我对解决方案感到惊讶,但它似乎确实有效,但您必须小心不要在打开和包含“using 语句”用法的作用域之间做任何事情#region#region

这没关系:

@{
    string baseUrl = "redacted";
    string requestUrl = "redacted";
    ...
    ...

    #region Redacted Region Name
        try
        {
            HttpWebRequest httpWebRequest = WebRequest.Create(requestUrl) as HttpWebRequest;
            httpWebRequest.Method = "GET";

            using (HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse)
            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                var data = reader.ReadToEnd();
                ...
            }
        }
        catch (Exception ex)
        {
            ...
        }
    #endregion
}

这存在语法错误,其中 Razor 认为关键字用于“using 指令”using

@{
    
    ...
    ...

    #region Redacted Region Name
        string baseUrl = "redacted";
        string requestUrl = "redacted";
        try
        {
            HttpWebRequest httpWebRequest = WebRequest.Create(requestUrl) as HttpWebRequest;
            httpWebRequest.Method = "GET";

            using (HttpWebResponse response = httpWebRequest.GetResponse() as HttpWebResponse)
            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                var data = reader.ReadToEnd();
                ...
            }
        }
        catch (Exception ex)
        {
            ...
        }
    #endregion
}