在的WebAPI方法返回HTTP 403 [英] Returning HTTP 403 in a WebAPI method

查看:255
本文介绍了在的WebAPI方法返回HTTP 403的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何返回从的WebAPI方法的HTTP 403?我试着抛出了Htt presponseException用的HTTPStatus code.Forbidden,我已经试过

How do I return an HTTP 403 from a WebAPI method? I've tried throwing an HttpResponseException with HttpStatusCode.Forbidden, and I've tried

return request.CreateErrorResponse(HttpStatusCode.Forbidden, pEx);

两者都不工作。双方始终返回HTTP 200。我在想什么?它必须是简单的东西,但我没有看到它。

Neither of which work. Both ALWAYS return an HTTP 200. What am I missing? It has to be something simple but I don't see it.

推荐答案

您可能与您的路由配置有问题。下面是一个工作样本。把它放在你的控制器,看看它是否工作。如果它不与诊断工具检查您的路由(即Cobisi路由助理)

You might have a problem with your routing configuration. Below is a working sample. Put it in your controller and see if it works. If it doesn't check your routing with a diagnostic tool (i.e. Cobisi Routing Assistant)

public string GetSomeString(int id)
{
    // This method is not allowed!
    return this.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "This method is not allowed!");
}

这篇关于在的WebAPI方法返回HTTP 403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆