在 MVC App_Code 显示助手中使用 C# 6 的使用静态语法

Using C# 6's using static syntax in MVC App_Code display helpers

是否可以在 MVC 5 App_Code 中的显示助手中使用 C# 6.0 的新 "using static" 符号?

我尝试这样做,但它似乎不喜欢这种语法。

我创建了一个静态助手 class 以在 App_Code 中公开 UrlHelper 和 HtmlHelper。我希望将它包含在 "using static" 语法中,以便使用 class 的属性,就好像文件有一个 ViewPage 基础 class.

@using static Example.Core.ViewHelper

@helper ExampleUsingUrl() {
    @Url.Action("Index", "Home")
}

ViewHelper 看起来像这样。

namespace Example.Core
{
    public static class ViewHelper
    {
        public static HtmlHelper Html { get; } = new HtmlHelper(new ViewContext(), new ViewDataContainer());
        public static UrlHelper Url => new UrlHelper(HttpContext.Current.Request.RequestContext, RouteTable.Routes); 
    }

    public class ViewDataContainer : IViewDataContainer
    {
        public ViewDataDictionary ViewData
        {
            get
            {
                return new ViewDataDictionary();
            }
            set
            {

            }
        }
    }
}

这个功能还不能查看吗?我正在使用 Visual Studio 2015、MVC 5、.NET 4.6.1 和 C# 6.0。

这在 MVC5 中是不可能的,但在 MVC6 中是不可能的。

查看代码更改以支持在 MVC6 中使用静态:https://github.com/aspnet/Razor/commit/1879ac642754b5f84e6055580e6fc60e13fa2100

我想我正在使用 mvc 5,它对我有用。 System.Web.Mvc.dll 版本 5.2.3.0

我是 运行 在使用 c# 6.0 的 VS 2015 中。所以要么是那个,要么是版本 5.2.3.0 正在做。

所以凯文的回答可能不是 100% 正确。

从广义上讲,MVC 6 不是 "stable",因此我们使用 MVC5 atm。

请注意,起初它没有找到我的静态引用,但没有波浪形。在剪切并重新粘贴“@using static”行几次后,它开始工作了。我可以导航到我的静态扩展方法的定义。