简体   繁体   English

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

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

I don't know if it is possible, I tried many ways but nothing seems to work in the project I am working on. 我不知道是否有可能,我尝试了很多方法,但是在我正在从事的项目中似乎没有任何效果。

Here is the deal : I've got an ASPX page that need to call a Web Service when a button is click. 这是交易:我有一个ASPX页面,当单击按钮时,该页面需要调用Web服务。 I can't use code behind and Page Method because my page can't inherit from 我无法在页面后面使用代码和Page Method,因为我的页面无法继承

System.Web.UI.Page

And the convention at my work doesn't let us use code behind. 我工作中的约定不允许我们在后面使用代码。

I've tried calling a ".cs" class with method like Ajax (Asynchronously and synchronously) with the "XMLHttpRequest". 我尝试使用“ XMLHttpRequest”通过Ajax之类的方法(异步和同步)调用“ .cs”类。 In my stand alone it worked fine but when I integrate it in the project it did not work (Iframe inside Iframe inside Iframe inside Master Page). 在我自己的立场上,它可以正常工作,但是当我将其集成到项目中时,它无法工作(Iframe内部的Iframe和母版页中的Iframe)。

I can't use things like external Libraries (Jquery, ...). 我不能使用外部库(Jquery等)之类的东西。 I'd like to know if inside the ASPX page I'm working on I can call with some other way my class.cs with the code I try to call. 我想知道我正在处理的ASPX页面中是否可以通过其他方式调用class.cs和尝试调用的代码。

I have : 我有 :

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

My JavaScript do some treatment (getting the text I need without all the DOM component like , , and only keep text inside 我的JavaScript做了一些处理(在没有所有DOM组件的情况下获取所需的文本,而只将文本保留在其中

) then is supposed to call the server part with the call for the Web Service (I can't call the Web Service from JS because of the cross domain Issue). ),然后应该通过Web服务的调用来调用服务器部分(由于跨域问题,我无法从JS调用Web服务)。

And my class.cs calling the Web Service is : 我的调用Web服务的class.cs是:

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

I have no more idea how to do it, I'm a beginner in ASP.NET (I used to work in JEE). 我不知道该怎么做,我是ASP.NET的初学者(我以前在JEE中工作)。 So if anyone has an idea or some tutorial that I can look to help me I'll gladly take it. 因此,如果任何人有想法或教程可以帮助我,我都会很乐意接受。

Thanks for your time trying to help me. 感谢您抽出宝贵的时间来帮助我。

If you are trying to use C# in your aspx page without a code behind (not recommended) you can just add it in a script tag in your aspx page. 如果您尝试在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" />

Or 要么

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

In your Aspx form, javascript function like below, 在您的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*/
                              }
                          });
                                       }

In your Aspx Form, code behind, 在您的Aspx表单中,编写代码,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM