在MVC App_Code显示帮助器中使用C#6使用静态语法 [英] Using C# 6's using static syntax in MVC App_Code display helpers

查看:77
本文介绍了在MVC App_Code显示帮助器中使用C#6使用静态语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to use C# 6.0's new "using static" notation inside of display helpers in App_Code in MVC 5?

我尝试这样做,但是它

我创建了一个静态帮助器类以在App_Code中公开UrlHelper和HtmlHelper。我希望将其包含在 using static语法中,以便像使用文件具有ViewPage基类一样使用类的属性。

I created a static helper class to expose UrlHelper and HtmlHelper within App_Code. I was hoping to include it with the "using static" syntax in order to use the properties of the class as I would as if the file had a ViewPage base class.

@using static Example.Core.ViewHelper

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

使用这样的ViewHelper。

With a ViewHelper looking like this.

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。

Is this feature not available for views yet? I am using Visual Studio 2015, MVC 5, .NET 4.6.1 and C# 6.0.

推荐答案

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

This is not possible in MVC5 but MVC6.

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

See code change for supporting using static in MVC6: https://github.com/aspnet/Razor/commit/1879ac642754b5f84e6055580e6fc60e13fa2100

这篇关于在MVC App_Code显示帮助器中使用C#6使用静态语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆