c# - 为什么 SuppressFormsAuthenticationRedirect 导致构建错误?

标签 c# asp.net-mvc-4 system.web

我在 this post 上找到了属性 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 立即设置它,这没有导致错误,并且按预期工作。那么为什么这是一个构建错误呢?我这样做只是为了好玩,并发现它按预期工作(但这很老套):

Response.StatusCode = (int)HttpStatusCode.Unauthorized;
((dynamic)Response).SuppressFormsAuthenticationRedirect = true;

最佳答案

Amith George 在评论中建议这是因为我的机器上安装了 .NET 4.5,但目标是 .NET 4.0。因为 .NET 4.5 是对 4.0 的就地升级,所以正在使用该 DLL,因此该变量在运行时具有 SuppressFormsAuthenticationRedirect 属性。构建正常失败,因为在针对 .NET 4 进行编译时,它不知道该属性。

关于c# - 为什么 SuppressFormsAuthenticationRedirect 导致构建错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17902419/

相关文章:

c# - 使用 .NET 代码将系统时间同步到域 Controller

jquery - Ajax 将下拉列表和复选框的值从 jquery 数据表发送到 Controller

asp.net - ASP.NET SessionState 中 "InProc"和 "stateServer"模式之间的差异

c# - 属性中的通用类型类名

c# - 从另一个线程关闭表单

ASP.NET MVC 4 - CMS/CMS 组件(如 Wordpress)

c# - 直接从 HttpServer 返回响应

c# - 为什么不编译多次使用 PreApplicationStartMethodAttribute?

c# - 发出同步 AJAX 请求时 IUriToStreamResolver 中的死锁

asp.net-mvc - 显示 Htmlhelper 的 Razor View 不包含定义