asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在ASP.NET MVC4中是否可以将C#或VB函数标记为Javascript?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 HtmlHelper扩展方法,它将javascript回调函数作为参数.例如:
@Html.SomethingCool("containerName","jsCallBACkFunction")

<script type="javascript">
    function jsCallBACkFunction(e,i) {
        alert(e.target.name + ' / ' + i);
    }
</script>

如您所见,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

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:SomethingCool是一个HtmlHelper扩展方法,我会写,输出html和javascript. JavaScript输出的一部分需要调用一个用户(程序员)来进行一些决定.想想它类似于您为ajax调用提供的成功或失败功能.

解决方法

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

这是基础设施代码.您需要完成getArgumentLiteral和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,new()
{
    protected CoreJSFunction() : base(null) {}
}

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

public class alert : CoreJSFunction<alert,alert.signature>
{
    public delegate void signature(String messagE);
}

这里有几个示例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!' : ''") {}
}

这是一个演示其使用的控制台应用程序:

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();
    }
}

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

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!');

更新:

您可能还想查看JSIL.我不隶属于该项目,不能说它的稳定性,准确性和效力,但它听起来很有趣,可能可以帮助您.

大佬总结

以上是大佬教程为你收集整理的在ASP.NET MVC4中是否可以将C#或VB函数标记为Javascript?全部内容,希望文章能够帮你解决在ASP.NET MVC4中是否可以将C#或VB函数标记为Javascript?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。