我有一个用c#/ASP.NET编写的Web服务.

[WebService(Namespace = "http://example.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class Service: System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Result GetData()
    {
        User user = GetUser();

        if (user.LoggedIn)
        {
            return GetData();
        }
        else
        {
            // raise exception -> return error 403
        }
    }

怎么可能从该Web服务返回错误403呢?我可以抛出一个异常-但这显示的是经验,而不是他的错误.

有什么 idea 吗?

推荐答案

完整回答这个问题——这是我使用的代码(感谢strider提供更多信息):

[WebService(Namespace = "http://example.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
[System.ComponentModel.ToolboxItem(false)]
public class Service: System.Web.Services.WebService
{

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Result GetData()
    {
        User user = GetUser();

        if (user.LoggedIn)
        {
            return GetData();
        }
        else
        {
            Context.Response.Status = "403 Forbidden"; 
            //the next line is untested - thanks to strider for this line
            Context.Response.StatusCode = 403;
            //the next line can result in a ThreadAbortException
            //Context.Response.End(); 
            Context.ApplicationInstance.CompleteRequest(); 
            return null;
        }
    }

Asp.net相关问答推荐

DBSet 不包含 Where 的定义

Thread.CurrentPrincipal 错误地声称是匿名的

如何从 JavaScript 调用 C# 函数?

SignalR 不在服务器上使用 Session

RestSharp 获取请求的完整 URL

ASP.NET 控件无法在 Visual Studio 2008 的代码隐藏中引用

异步编程与线程有什么不同?

ASP.net 页面在导入语句上出现错误,但我确实有参考?

如何在 Visual Studio 2017 中使用 NPM 并安装包?

Gridview ItemTemplate 中多个判断字段的最佳技术?

无法确定条件表达式的类型,因为 'string' 和 'System.DBNull' 之间没有隐式转换

如何从 Asp.net Mvc-3 发送邮箱?

如何以编程方式将标签的前景色设置为其默认值?

global.asax 中的 Application_Error 未捕获 WebAPI 中的错误

在c#中显示带有换行符的标签文本

如何使用 jquery 设置单选按钮 Select 的值

如何解决我的 ASP.Net MVC 应用程序中的 iisreset 后发生的 AntiForgeryToken 异常?

如何使用 int ID 列更改 ASP.net Identity 2.0 的表名?

使用 FileUpload Control 获取文件的完整路径

在没有等待 #2 的情况下调用异步方法