在asp.net中使用webmethod [英] using the webmethod in asp.net
本文介绍了在asp.net中使用webmethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在使用Web方法进行编程,当我想调用response.redirected时,我收到了线程异常的错误,所以请
帮助以下是代码.
Hi
i am using the web method my programing and when i wanted to call response.redirected then i am getting the error that the thread exception so please
help the following is the code.
[WebMethod]
static public string UpdateUser(string roomID)
{
try
{
ChatRoom room = ChatEngine.GetRoom(roomID);
if (room != null)
{
string res = "";
if (room != null)
{
string s = string.Empty;
//if ((HttpContext.Current.Session["list"] != null) && (HttpContext.Current.Session["list"] != ""))
//{
// s = HttpContext.Current.Session["list"].ToString();
// foreach (char data in s)
// {
// if (data == Convert.ToChar(HttpContext.Current.Session["UserName"]))
// {
// res = room.UpdateUser(HttpContext.Current.Session["UserName"].ToString(), data);
// }
// }
//}
//else
//{
res = room.UpdateUser(HttpContext.Current.Session["UserName"].ToString());
string dataset3 = rid;
ChatRoom room1 = ChatEngine.GetRoom(dataset3.ToString());
string prevMsgs = room1.JoinRoom1(HttpContext.Current.Session["UserName"].ToString(), HttpContext.Current.Session["UserName"].ToString());
string dataset = string.Empty;
Clslogin clslogin = new Clslogin();
DateTime ds1 = DateTime.Now.Date;
string ds = ds1.ToString("MM/dd/yyyy") + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute;
if ((HttpContext.Current.Session["UserName"] != null) || (HttpContext.Current.Session["UserName"] .ToString()!= ""))
{
int userid=Convert.ToInt32(clslogin.groupuserid(HttpContext.Current.Session["UserName"].ToString()));
dataset = clslogin.usermessage(userid, ds, rid.ToString());
if (dataset != "")
{
HttpContext.Current.Response.Redirect("individual.aspx?Channel=" + rid.ToString() + "&User=" + HttpContext.Current.Session["UserName"].ToString() + "", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
//HttpContext.Current.Response.Write("<script type='text/javascript'>detailedresults=window.open('individual.aspx?Channel=" + rid.ToString() + "&User=" + HttpContext.Current.Session["UserName"].ToString() + "',null,'height=300,width=600,resizable=no,scrollbars=no,titlebar=no,status=no,toolbar=no,menubar=no,channelmode=no');</script>");
}
else
{
}
}
else
{
}
//}
}
return res;
}
}
catch (Exception ex)
{
}
return "";
}
推荐答案
如果您在try catch
块中调用Response.Redirect
,则必须将调用的第二个参数设置为false,否则,您可以会得到一个ThreadAbortException
.
此外,Web应用程序中的WebMethod类似于桌面应用程序中类库中的方法.它无法控制UI.理想情况下,它应该向调用方返回一个值,并且调用方将必须进行重定向.
If you callResponse.Redirect
within atry catch
block, you must set the second argument of the call to false, otherwise you''ll get aThreadAbortException
.
Moreover, a WebMethod in a web app is analogous to a method from a class library in a desktop application. It cannot control the UI. It should ideally return a value to the caller and the caller will have to do the redirect.
这篇关于在asp.net中使用webmethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文