C# Asp.Net:Web服务抛出;“线程正在中止”;
我有一个通过ajax调用的web服务,它要求用户登录。在每种方法中,我都想检查用户是否登录,如果没有,则发送403代码,但是当我调用C# Asp.Net:Web服务抛出;“线程正在中止”;,c#,asp.net,web-services,C#,Asp.net,Web Services,我有一个通过ajax调用的web服务,它要求用户登录。在每种方法中,我都想检查用户是否登录,如果没有,则发送403代码,但是当我调用Response.End()。我应该打什么电话 [WebMethod(true)] public string MyMethod() { if(!userIsLoggedIn) { HttpContext.Current.Response.StatusCode = 403; HttpContext.Cu
Response.End()
。我应该打什么电话
[WebMethod(true)]
public string MyMethod()
{
if(!userIsLoggedIn)
{
HttpContext.Current.Response.StatusCode = 403;
HttpContext.Current.Response.End();
}
/* Do stuff that should not execute unless the user is logged in... */
...
}
从:
如果使用Response.End,
Response.Redirect或Server.Transfer
方法,一个ThreadAbortException
出现异常。你可以使用
请尝试catch语句来捕获此消息
例外
这种行为是故意的
要解决此问题,请使用一个
以下方法之一:
因为它是一个
WebMethod
,所以最简单的方法就是什么也不返回。这相当于结束对ajax请求的响应:
[WebMethod(true)]
public string MyMethod()
{
if(!userIsLoggedIn)
{
HttpContext.Current.Response.StatusCode = 403;
return null;
}
}
如果我对不可为null的类型返回null,我会抛出错误吗?@Master-它不应该让你编译它,但为了安全起见,你可以抛出
default(typeHere)
。