如何创建从WCF REST方法的自定义HTTP状态代码? [英] How can I create a custom HTTP status code from a WCF REST method?
问题描述
我想从当某个条件满足一个RESTful的WCF web服务返回自定义HTTP状态代码。
I would like to return a custom HTTP status code from a RESTful WCF web service when a certain condition is met.
我不想使用标准的HTTP状态代码;相反,我想回例如,一个514状态码,这意味着一些东西到主叫用户。
I do not want to use the standard HTTP status codes; instead, I would like to return e.g., a 514 status code which means something to the calling user.
这可能与 WebOperationContext.Current。 OutgoingResponse
?
推荐答案
的 OutgoingResponse.StatusCode 是在那里你会设置状态码,但它是一个的 HttpStatusCode 枚举,而不是一个整数值。
OutgoingResponse.StatusCode is where you'd set the status code, but it's an HttpStatusCode enum, not an integer value.
您可以施放一个自定义值的int HttpStatusCode,但我不知道该框架将用它来做;它很可能会抛出,但不能伤害尝试。
You could cast a custom value int to HttpStatusCode, but I'm not sure what the framework would do with it; most likely it would throw, but it can't hurt to try.
尽管HTTP规范不会不接受定制响应码,它可能不是一个好主意除非你必须在路线。也许一个自定义的响应头会更好?
Although the HTTP spec doesn't disallow custom response codes, it's probably not a good idea to go that route unless you must. Perhaps a custom response header would be better?
如果你必须去自定义状态代码路径,另一个选择可能是使用ASP.NET兼容模式。这将允许您使用 HttpContext.Current.Response.StatusCode ,这是一个int。你不得不在IIS但主办这个工作,我不知道你的架构。
If you must go the custom status code route, another option might be to use ASP.NET compatibility mode. That would allow you to use HttpContext.Current.Response.StatusCode, which is an int. You'd have to be hosted in IIS for this to work, however, and I don't know your architecture.
这篇关于如何创建从WCF REST方法的自定义HTTP状态代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!