是否可以在ASP.NET MVC4中将输出标记为C#或VB函数作为Javascript?

Sam*_*Axe 10 javascript c# vb.net asp.net-mvc-4 razor-2

我有一个HtmlHelper扩展方法,它将javascript回调函数作为参数..例如:

@Html.SomethingCool("containerName", "jsCallbackFunction")

<script type="javascript">
    function jsCallbackFunction(e, i) {
        alert(e.target.name + ' / ' + i);
    }
</script>
Run Code Online (Sandbox Code Playgroud)

如您所见,javascript回调函数名称将传递给HtmlHelper扩展方法.这导致开发人员必须返回文档以确定jsCallbackFunction函数需要哪些参数.

我更喜欢这样的东西:

@Html.SomethingCool("containerName", New SomethingCoolCallbackDelegate(Address Of jsCallbackFunction))

<OutputAsJavascript>
Private Sub jsCallbackFunction(e, i)
    '    SOMETHING goes here.  some kind of html dom calls or ???
End Sub
Run Code Online (Sandbox Code Playgroud)

SomethingCoolCallbackDelegate将提供目标函数的代码契约.然后编译器会在MVC页面上将jsCallbackFunction编译为javascript.

.NET 4/ASP.NET MVC 4/Razor 2中是否有这样的内容?或任何其他可以实现类似的技术?

示例在VB中,但C#中的解决方案也是可以接受的.

澄清:

@gideon:注意jsCallbackFunction两个参数e,和i.但是,HtmlHelper扩展方法只是要求一个字符串(javascript回调函数的名称),并不指示此函数可能采用的参数.我试图解决的问题是双重的.

  • 首先,缺少参数提示.传递代替"javascript回调名称"字符串的.NET委托类型将实现此目的.我愿意接受其他解决方案来实现这一目标.我知道XML注释.它们并不是真正的解决方案.

  • 其次,尝试让页面编程人员使用单一语言.在javascript和VB(或js和C#)之间切换需要(对我来说至少)昂贵的上下文切换.我的大脑没有快速过渡.让我在VB或C#中工作更具生产力和成本效益.因此,能够在.NET语言中编写函数并将其编译为javascript,在ASP.NET MVC/razor视图的上下文中,就是我在这里所追求的.

@TyreeJackson:我会编写一个输出html和javascript SomethingCoolHtmlHelper扩展方法.部分javascript输出需要调用用户(程序员) - 提供的函数来做出一些决定.可以认为它类似于您为ajax调用提供的成功或失败功能.

Tyr*_*son 5

虽然我不能给你一个完整的转换器/编译器选项,因为这将是一项大量的工作,我可以建议以下内容来协助intellisense支持和发出功能和调用.

这是基础设施代码.您需要完成getArgumentLite和getConstantFromArgument函数来处理您提出的其他情况,但这是一个不错的起点.

public abstract class JavascriptFunction<TFunction, TDelegate> where TFunction : JavascriptFunction<TFunction, TDelegate>, new()
{
    private static  TFunction   instance    = new TFunction();
    private static  string      name        = typeof(TFunction).Name;
    private         string      functionBody;

    protected JavascriptFunction(string functionBody) { this.functionBody = functionBody; }

    public static string Call(Expression<Action<TDelegate>> func)
    {
        return instance.EmitFunctionCall(func);
    }

    public static string EmitFunction()
    {
        return "function " + name + "(" + extractParameterNames() + ")\r\n{\r\n    " + instance.functionBody.Replace("\n", "\n    ") + "\r\n}\r\n";
    }

    private string EmitFunctionCall(Expression<Action<TDelegate>> func)
    {
        return name + "(" + this.extractArgumentValues(((InvocationExpression) func.Body).Arguments) + ");";
    }

    private string extractArgumentValues(System.Collections.ObjectModel.ReadOnlyCollection<Expression> arguments)
    {
        System.Text.StringBuilder   returnString    = new System.Text.StringBuilder();
        string                      commaOrBlank    = "";
        foreach(var argument in arguments)
        {
            returnString.Append(commaOrBlank + this.getArgumentLiteral(argument));
            commaOrBlank    = ", ";
        }
        return returnString.ToString();
    }

    private string getArgumentLiteral(Expression argument)
    {
        if (argument.NodeType == ExpressionType.Constant)   return this.getConstantFromArgument((ConstantExpression) argument);
        else                                                return argument.ToString();
    }

    private string getConstantFromArgument(ConstantExpression constantExpression)
    {
        if (constantExpression.Type == typeof(String))  return "'" + constantExpression.Value.ToString().Replace("'", "\\'") + "'";
        if (constantExpression.Type == typeof(Boolean)) return constantExpression.Value.ToString().ToLower();
        return constantExpression.Value.ToString();
    }

    private static string extractParameterNames()
    {
        System.Text.StringBuilder   returnString    = new System.Text.StringBuilder();
        string                      commaOrBlank    = "";

        MethodInfo method = typeof(TDelegate).GetMethod("Invoke");
        foreach (ParameterInfo param in method.GetParameters())
        {
            returnString.Append(commaOrBlank  + param.Name);
            commaOrBlank = ", ";
        }
        return returnString.ToString();
    }
}

public abstract class CoreJSFunction<TFunction, TDelegate> : JavascriptFunction<TFunction, TDelegate>
    where TFunction : CoreJSFunction<TFunction, TDelegate>, new()
{
    protected CoreJSFunction() : base(null) {}
}
Run Code Online (Sandbox Code Playgroud)

以下是标准函数支持包装器的示例:

public class alert : CoreJSFunction<alert, alert.signature>
{
    public delegate void signature(string message);
}
Run Code Online (Sandbox Code Playgroud)

以下是一些示例Javascript函数支持包装器:

public class hello : JavascriptFunction<hello, hello.signature>
{
    public delegate void signature(string world, bool goodByeToo);
    public hello() : base(@"return 'Hello ' + world + (goodByeToo ? '. And good bye too!' : ''") {}
}

public class bye : JavascriptFunction<bye, bye.signature>
{
    public delegate void signature(string friends, bool bestOfLuck);
    public bye() : base(@"return 'Bye ' + friends + (bestOfLuck ? '. And best of luck!' : ''") {}
}
Run Code Online (Sandbox Code Playgroud)

这是一个控制台应用程序,展示其用途:

public class TestJavascriptFunctions
{
    static void Main()
    {
        // TODO: Get javascript functions to emit to the client side somehow instead of writing them to the console
        Console.WriteLine(hello.EmitFunction() + bye.EmitFunction());

        // TODO: output calls to javascript function to the client side somehow instead of writing them to the console
        Console.WriteLine(hello.Call(func=>func("Earth", false)));
        Console.WriteLine(bye.Call(func=>func("Jane and John", true)));
        Console.WriteLine(alert.Call(func=>func("Hello World!")));

        Console.ReadKey();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是控制台应用程序的输出:

function hello(world, goodByeToo)
{
    return 'Hello ' + world + (goodByeToo ? '. And good bye too!' : ''
}
function bye(friends, bestOfLuck)
{
    return 'Bye ' + friends + (bestOfLuck ? '. And best of luck!' : ''
}

hello('Earth', false);
bye('Jane and John', true);
alert('Hello World!');
Run Code Online (Sandbox Code Playgroud)

更新:

您可能还想查看JSIL.我不参与该项目,不能说它的稳定性,准确性和功效,但听起来很有趣,并且可以帮助你.