Asp.Net Web 服务:我想返回错误 403 forbidden [英] Asp.Net web service: I would like to return error 403 forbidden
本文介绍了Asp.Net Web 服务:我想返回错误 403 forbidden的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用 c#/asp.net 编写的网络服务.
I have got a web service programmed in c# / asp.net.
[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?我可以抛出异常 - 但这显示的是异常而不是他的错误.
How is it possible to return error 403 out of this web service? I can throw an exception - but this shows the exeption and not his error.
有什么想法吗?
推荐答案
完整回答这个问题 - 这是我使用的代码(感谢 strider 提供更多信息):
To answer the question completely - this is the code I've used (thank you strider for more information):
[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 Web 服务:我想返回错误 403 forbidden的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文