标签: webmethod

从JavaScript调用ASP.NET Web服务方法

我有网络服务:

[WebMethod]
public void SendMail(string _name, string _email, string _message)
{
    //Create Mail Message Object with content that you want to send with mail.
    MailMessage MyMailMessage = new MailMessage("gglebati@example.com", "gglebati@example.com", "This is the mail subject", "Just wanted to say Hello");

    MyMailMessage.IsBodyHtml = false;

    //Proper Authentication Details need to be passed when sending email from gmail
    NetworkCredential mailAuthentication = new NetworkCredential("myxxxxx@gmail.com", "xxxxxxxxx");

    //Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587
    //For different server like yahoo this …
Run Code Online (Sandbox Code Playgroud)

javascript asp.net ajax web-services webmethod

1
推荐指数
1
解决办法
1万
查看次数

从ASPX代码背后调用Web方法

我试图找出一种方法,我可以从另一个ASPX Code Behind页面调用Web方法(位于aspx页面后面的代码中作为公共静态方法).

这是Page A.aspx背后的代码

public partial class PageA : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    // other code not relevant is here  
    }

    [WebMethod(true)]
    public static string GetStringInfo()
    {
         // do some stuff here to build my string
         // return string
    }
}
Run Code Online (Sandbox Code Playgroud)

在页面B上,我需要能够在页面加载或其他事件期间调用GetStringInfo()来获取信息.GetStringInfo()相当复杂,并且出于我无法控制的原因,无法将其移动到其他地方或目前重建.

如何从其他页面的代码中使用上面的Web方法?

我已经尝试实例化其他页面(PageB)的副本,例如:

public partial class PageB : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       PageA page = new PageA();
       page.GetStringInfo();        
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的问题是因为它是动态编译的,所以我没有一个简单的命名空间来引用和访问.我试过添加一个,它忽略了它.

该项目位于.net 3.5,C#,是一个网站项目(不是Web应用程序).

任何帮助表示赞赏!

c# asp.net webmethod

1
推荐指数
1
解决办法
1万
查看次数

c#中的等于是真的

我在c#中做webmethod.在调试时,

(chk.Equals(oldpass))
Run Code Online (Sandbox Code Playgroud)

查询在左侧和右侧显示相同的值.

但是,如果执行移动到显示return语句的else部分,而不是进入内部.FOLL.是我的代码.

[WebMethod (Description="for change in password")]
public string update(string authenid,string oldpass,string newpass)
{
     SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\Workspace\\visual studio workspace\\Tmrepo\\App_Data\\tmrepo.mdf;Integrated Security=True;User Instance=True");

    try
    {
        conn.Open();

  string chk = "select pwd from client where authenid = '"+ @authenid +"' ";
        if(chk.Equals(oldpass))
        {
            string update = "update client set pwd=@newpass where pwd=@oldpass and authenid=@authenid";
            SqlCommand cmd = new SqlCommand(update, conn);
            cmd.Connection = conn;
            cmd.Parameters.AddWithValue("@authenid", authenid);
            cmd.Parameters.AddWithValue("@oldpass", oldpass);
            cmd.Parameters.AddWithValue("@newpass", newpass);
            cmd.ExecuteNonQuery();

        }

        else
        {
            return "invalid oldpass";
        }
 conn.Close(); …
Run Code Online (Sandbox Code Playgroud)

c# equals webmethod

1
推荐指数
1
解决办法
207
查看次数

AJAX到web方法不返回JSON

我使用AJAX从我的js文件中调用aspx页面中的web方法.我已将方法设置为[WebMethod],页面继承自System.Web.Ui.Page类.它仍然没有将JSON格式返回给我的调用ajax函数.

这是js文件中的AJAX调用:

         $.ajax({
                 type: "POST",
                 url: "/WebServiceUtility.aspx/CustomOrderService",
                 data: "{'id': '2'}",
                 contentType: "application/json; charset=utf-8",
                 dataType: "json",
                 success: function (message) {
                     ShowPopup(message);
                 }
               });
         function ShowPopup(result) {
             if (result.d != "") {
                 request=result.d;
             }
         }
Run Code Online (Sandbox Code Playgroud)

这是web方法:

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Services;

namespace SalesDesk.Global
{
public partial class WebServiceUtility : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

        [WebMethod]
        public string CustomOrderService(string id)
        {
            string result;
            // code logic which sets the result value
            result="some value"; …
Run Code Online (Sandbox Code Playgroud)

asp.net jquery json webmethod

1
推荐指数
2
解决办法
1万
查看次数

ASP.NET:通过 Web 方法获取当前 URL?

我正在尝试通过 Web 方法检索当前页面的 URL。下面的代码适用于普通的 C# 方法,例如 Page_Load,但不适用于 Web 方法。

[WebMethod(EnableSession=true)]
public static void UpdateProjectName(string name)
{
        string project_id = HttpContext.Current.Request.Url.ToString();
}
Run Code Online (Sandbox Code Playgroud)

我收到一个空字符串 ("") 作为 project_id。我究竟做错了什么?

c# asp.net web-services webmethod asp.net-web-api

1
推荐指数
1
解决办法
5517
查看次数

WebMethod 异步结果显示对象

我正在尝试使用 async 返回结果,但我希望它返回整个对象而不是返回数据

[System.Web.Services.WebMethod(BufferResponse=false)]
public static async Task<bool> getLogin(string username, string password)
{
    Login login = new Login();
    Task<bool> loginVerify = login.verifyLogin(username,password);
    await loginVerify;
    return loginVerify.Result;
}

public class Login
{
    public async Task<bool> verifyLogin(string username, string password)
    {
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

Firefox Firebug 的结果显示:

{"d":{"Result":true,"Id":2,"Exception":null,"Status":5,"IsCanceled":false,"IsCompleted":true,"CreationOptions":0,"AsyncState":null,"IsFaulted":false}}
Run Code Online (Sandbox Code Playgroud)

为什么不只是显示结果?

我试过跑步

public static async Task<bool> getLogin(string username, string password)
{
    Login login = new Login();
    Task<bool> loginVerify = login.verifyLogin(username,password);
    await loginVerify;
    return false;
}
Run Code Online (Sandbox Code Playgroud)

但是萤火虫报告是相同的,只是它在 json 中说 Result false

{"d":{"Result":false,"Id":2,"Exception":null,"Status":5,"IsCanceled":false,"IsCompleted":true,"CreationOptions":0,"AsyncState":null,"IsFaulted":false}}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是为什么它显示整个对象而不是结果?

c# jquery webmethod async-await

1
推荐指数
1
解决办法
785
查看次数

从AJAX WebMethod返回Gridview

在一段时间内,我意识到通过使用AJAX Control Toolkit,您最终可以做的不仅仅是使用jquery.所以我开始使用jquery ajax而不是updatepanels等控件,它看起来更快,更简单.

我有一个问题是,我在updatepanel内部有一个gridview with paging,并且有一个referh按钮,它从DB中取出行并再次绑定gridview.现在我想使用webmethod.

有没有办法从webmethod返回这个?还有很多其他情况可以说我在updatepanel里面有一个.ascx控件.有什么方法可以返回这样的控件吗?任何示例链接都赞赏

谢谢

asp.net ajax jquery webmethod

0
推荐指数
1
解决办法
2764
查看次数

ASP.NET C# - 实时通知方法

你认为这是一种检索通知的坏方法吗?这个WebMethod将通过JavaScript每10秒调用一次.有没有更好的方法呢?如果是这样请详细说明.谢谢.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public int[] GetNotifications()
{
    int[] Notifications = new int[3]{0, 0, 0};
    if (HttpContext.Current.User.Identity.IsAuthenticated) {
        string UserName = HttpContext.Current.User.Identity.Name;
        Notifications[0] = Notification.GetAll(UserName, false).Count;
        Notifications[1] = Message.GetAll(UserName, false).Count;
        Notifications[2] = Friendship.GetFriends(UserName, true).Count;
    }
    return Notifications;
}
Run Code Online (Sandbox Code Playgroud)

javascript c# asp.net asmx webmethod

0
推荐指数
1
解决办法
2203
查看次数

如何使用Jquery从ASP.Net WebMethod调用返回HTML?

使用Asp.Net 4.0 Web窗体和Jquery 1.6.2.我想在页面上对WebMethod进行Ajax调用,并让它返回html.在服务器端,WebMethod看起来像这样.

[WebMethod]
public static string GetOrders()
{
    return theMethodThatGeneratesHtml();
}
Run Code Online (Sandbox Code Playgroud)

这是调用Ajax函数.

function GetOrders()
{
    $.ajax({
        type: 'POST',
        contentType: "application/json",
        url: "Orders.aspx/GetOrders",
        data: "{}",
        success: function (data) {
            $('#content').html(data);
        },
        dataType: "text"
    });
}
Run Code Online (Sandbox Code Playgroud)

从WebMethod返回的数据总是被包装为以这样开始的json对象.

{"d":"\u003ctable\u003e\r\n ........   
Run Code Online (Sandbox Code Playgroud)

如何让WebMethod只返回HTML?

asp.net jquery webmethod

0
推荐指数
1
解决办法
1万
查看次数

为什么我的交易不起作用?

我有一个方法(带WebMethod属性),我定义了一个事务,在我的方法和我的事务中我调用了2个存储过程,第一个是GetAllBook:

select * 
from Book_TBL 
where IsActive = 'True'
Run Code Online (Sandbox Code Playgroud)

第二个是updateBook:

update Book_TBL 
set IsActive = 'False' 
where IsActive = 'True'
Run Code Online (Sandbox Code Playgroud)

和我的方法:

 public struct BOOK
 {
        public string BOOK_NAME;
        public string BOOK_DESC;          
 }

[WebMethod]
public static List<BOOK> GetMyBooks()
{
    using (TransactionScope _transactionScope = new TransactionScope(TransactionScopeOption.Required))
    {
        string _connString = "Data Source=.;Initial Catalog=BookStore;Integrated Security=True";
        SqlConnection _conn = new SqlConnection(_connString);
        _conn.Open();

        SqlCommand _com = new SqlCommand();
        _com.CommandType = System.Data.CommandType.StoredProcedure;
        _com.CommandText = "GetAllBook";
        _com.Connection = _conn;

        SqlDataAdapter bookdataAdapter …
Run Code Online (Sandbox Code Playgroud)

c# asp.net webmethod

0
推荐指数
1
解决办法
76
查看次数