C# 为什么SuppressFormsAuthenticationRedirect会导致生成错误?

C# 为什么SuppressFormsAuthenticationRedirect会导致生成错误?,c#,asp.net-mvc-4,system.web,C#,Asp.net Mvc 4,System.web,我在上找到属性SuppressFormsAuthenticationRedirect,但当我尝试使用它时: Response.StatusCode = (int)HttpStatusCode.Unauthorized; Response.SuppressFormsAuthenticationRedirect = true; 我得到一个生成错误: Error 53 'System.Web.HttpResponseBase' does not contain a definition for

我在上找到属性
SuppressFormsAuthenticationRedirect
,但当我尝试使用它时:

Response.StatusCode = (int)HttpStatusCode.Unauthorized;
Response.SuppressFormsAuthenticationRedirect = true;
我得到一个生成错误:

Error   53  'System.Web.HttpResponseBase' does not contain a definition for 'SuppressFormsAuthenticationRedirect' and no extension method 'SuppressFormsAuthenticationRedirect' accepting a first argument of type 'System.Web.HttpResponseBase' could be found (are you missing a using directive or an assembly reference?)   Controllers\ErrorController.cs  39  26  Roving
因此,我加入了一个断点,检查了watch窗口中的
Response
,发现它确实具有该属性。因此,我尝试使用
Response.SuppressFormsAuthenticationRedirect=true在immediate中设置它,但没有导致错误,并且它按预期工作。那么为什么这是一个构建错误呢?我这样做,只是为了好玩,发现它工作得和预期的一样(但这是相当粗糙的):


Amith George在评论中表示,这是因为我的机器上安装了.NET 4.5,但目标是.NET 4.0。由于.NET 4.5是4.0上的就地升级,因此正在使用该DLL,因此该变量在运行时具有SuppressFormsAuthenticationRedirect属性。生成正确失败,因为在根据.NET 4编译时,它不知道该属性。

您的项目是否设置为根据.NET 4而不是.NET 4.5进行生成?其次,如果您的应用程序一开始就不编译/生成,您如何能够到达调试部分?@AmithGeorge Yes,我确实看到它似乎只在4.5中受支持,但为什么该属性会在那里,并且在运行时可以访问?@AmithGeorge调试/检查它时没有那一行(注释掉)。这是因为您必须在本地计算机上安装.Net 4.5。VS认为最终的部署环境将只有.NET4.0,因此它阻止了构建。
Response.StatusCode = (int)HttpStatusCode.Unauthorized;
((dynamic)Response).SuppressFormsAuthenticationRedirect = true;