【问题标题】:ASP.net 5 RC1 - Can't Reference System.Web.MvcASP.net 5 RC1 - 无法引用 System.Web.Mvc
【发布时间】:2015-12-20 03:48:25
【问题描述】:

我只使用 Core CLR,这可能是我无法引用它的原因。

我的 project.json 中有以下内容:

...
"frameworks": {
    "dnxcore50": {
      "dependencies": {
        "EntityFramework.Commands": "7.0.0-rc1-final",
        ...
        "Microsoft.AspNet.Mvc": "6.0.0-rc1-final"
      }
    }
  },
...

我引用了 Microsoft.AspNet.Mvc,我相信它应该让我从我的代码中引用 System.Web.Mvc。但是没有。

我的控制器:

using System.Web.Mvc; // "Red underline under "Web"
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace MyNamespace
{
    [Authorize] // Red underlined
    public class UsersController : Controller
    {
        // Code inside not shown because it's not the issue.
    }
}

我正在尝试在我的一个 Web API 控制器中使用 [Authorize] 属性。


更新。我还需要引用新包 Microsoft.AspNet.Authorization。

大约半小时后,将鼠标悬停在带红色下划线的 [Authorize] 属性上,Intellisense 给了我参考 Microsoft.AspNet.Authorization 的建议。直到现在它才给我这个选项。

另外,我搜索了 ASP.net 5 的 Authorize 属性,所有结果和教程都告诉我参考 System.Web.Mvc。事实证明,所有这些教程都较旧。

【问题讨论】:

  • 请不要在您的问题中编辑解决方案。一旦宽限期到期,发布答案并将其标记为已检查,否则该问题将永远标记为打开,并且 stackoverflow 将选择它并显示在未回答的问题部分。这就是堆栈溢出的工作原理
  • 谢谢。我一直认为只有其他用户才能将我的回复标记为已回答。
  • 您也可以,但有 2 天(48 小时)的宽限期。查看帮助中心:stackoverflow.com/help/self-answer
  • @Mickael 对于您最近在另一个 stackexchange 网站上发布的问题,我有一些意见……出于某种原因,我不能在那里发表评论,我不想让它完整回答。您是否考虑在总部位于山景城的搜索引擎公司托管的电子邮件平台上通过“rofi cut me”向我发送电子邮件?

标签: asp.net-mvc authorization asp.net-core asp.net-core-mvc


【解决方案1】:

基于幸运的发现,我需要 Microsoft.AspNet.Authorization 才能使用 [Authorize]。没有教程甚至谷歌搜索都没有给我这个信息。

project.json:注意,新引用 Microsoft.AspNet.Authorization;

...
"frameworks": {
    "dnxcore50": {
      "dependencies": {
        "EntityFramework.Commands": "7.0.0-rc1-final",
        ...
        "Microsoft.AspNet.Authorization": "1.0.0-rc1-final",
        "Microsoft.AspNet.Mvc": "6.0.0-rc1-final"
      }
    }
  },
...

控制器:

// using System.Web.Mvc; // Removed this line - old. Possibly changed as far back as Beta8
using System.Threading.Tasks;
using Microsoft.AspNet.Authorization; // Added this line. It's new.
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace MyNamespace
{
    [Authorize] // No longer red-underlined. This is from Microsoft.AspNet.Authorization
    public class UsersController : Controller
    {
        // Code inside not shown because it's not the issue.
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-16
    • 2016-04-04
    • 1970-01-01
    • 2016-03-04
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多