如何在没有代码隐藏的情况下在ASP.NET中调用C#class.cs

how to call C# class.cs in ASP.NET without Code Behind

本文关键字:NET ASP C#class cs 情况下 调用 代码 隐藏      更新时间:2023-09-26

我不知道这是否可能,我尝试了很多方法,但在我正在进行的项目中似乎都不起作用。

交易是这样的:我有一个ASPX页面,当点击按钮时需要调用Web服务。我不能使用代码隐藏和页面方法,因为我的页面不能从继承

System.Web.UI.Page

我工作中的惯例不允许我们使用代码隐藏。

我尝试过用Ajax这样的方法(异步和同步)用"XMLHttpRequest"调用".cs"类。就我个人而言,它工作得很好,但当我将其集成到项目中时,它就不起作用了(Iframe在母版页内Iframe在Iframe内)。

我不能使用外部库(Jquery,…)之类的东西。我想知道在我正在处理的ASPX页面中,我是否可以用其他方式用我试图调用的代码调用我的类.cs。

我有:

<input type="button" onclick="callSomeJSfunction()" value="testC#" />

我的JavaScript做了一些处理(在没有所有DOM组件的情况下获得我需要的文本,并且只在

中保留文本),然后应该通过调用Web服务来调用服务器部分(由于跨域问题,我无法从JS调用Web服务)。

我的类.cs调用Web服务是:

namespace testProlexis
{
    public class prolexisWSCorrector
    {
        public static string callProlexis(string textToCorrect)
        {
            if (string.IsNullOrEmpty(textToCorrect))
            {
                throw new Exception("pas de texte à corriger pour Prolexis");
            }
            prolexisWebServiceImpl.ProLexisService test = new prolexisWebServiceImpl.ProLexisService();
            prolexisWebServiceImpl.AnalyzerInput inPut = new prolexisWebServiceImpl.AnalyzerInput();
            prolexisWebServiceImpl.AnalyzerOutput outPut = new prolexisWebServiceImpl.AnalyzerOutput();
            inPut.text = textToCorrect;
            String info ="try5";
            outPut = test.analyze(inPut, working);
            int tytytyty = outPut.errors.Count();
            return textToCorrect;
}

我不知道该怎么做,我是ASP.NET的初学者(我以前在JEE工作)。所以,如果有人有一个想法或一些教程,我可以寻求帮助我,我会很乐意接受它

感谢您花时间帮助我。

如果您试图在没有代码隐藏的情况下在aspx页面中使用C#(不推荐),您可以将其添加到aspx页面的脚本标记中。

<script language="C#" type="text/C#" runat="server">
    public void DoStuff(object sender, EventArgs e)
    {
        var proxy = new prolexisWSCorrector();
    }
</script>
<asp:Button ID="" runat="server" Click="DoStuff" />

<input type="submit" runat="server" onClick="DoStuff" />

在您的Aspx表单中,javascript函数如下,

         function callSomeJSfunction() {
               jQuery.ajax({
                        type: "POST",
                        url: "YourForm.aspx/YourPublicStaticWebMethod",
                        data: "{'ID': '" + txtID.value+ "','Value': '" + txtYear.value '}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                           /*Do Success*/
                              },
                       Error:function(er){
                            /*Do Error*/
                              }
                          });
                                       }

在您的Aspx表单中,代码隐藏,

[System.Web.Services.WebMethod]
public static Function YourPublicStaticWebMethod(int id,string year)
{
 //Code Here
}