ASP.NET MVC返回超过预期 [英] ASP.NET MVC returning more than expected

查看:196
本文介绍了ASP.NET MVC返回超过预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code,当达到一定发生异常,即时返回

In my code, when a certain exception happens, im returning

Response.AddHeader("X-Status", "çãõáéí");
Response.Charset = "utf-8";
Response.HeaderEncoding = Encoding.UTF8;
return new HttpStatusCodeResult(403, "You lack permission X, Y and Z");

和使用邮差我很惊讶地看到三件事情:

and using Postman I was surprised to see 3 things:


  1. 响应的主体。它应该是空的..响应应该只包含标题,但身体实际上是全HTML / CSS ...

  2. 内容类型头只是text / html的,而不是text / html的字符集= UTF-8

  3. 没有什么表明Response.HeaderEncoding设置...它是否被设置为UTF8或UTF32或大的印...

  1. the body of the response. it should be empty.. the response should contain only headers, but the body is actually full of html/css...
  2. the Content-Type header was only "text/html" and not "text/html charset=utf-8"
  3. there was nothing indicating that "Response.HeaderEncoding" was set... whether it was set to utf8 or utf32 or big-indian...

所以,我怎么能使身体是空的?也就是说,我希望响应的内容是明确的......加分,如果你能教我恩code X-状态所以浏览器显示特殊字符正确,而不是像一个倒挂的问号...

So, how can I "make" the body empty? meaning that I wish the content of the response to be clear... Extra points if you are able to teach me to encode X-Status so the browser displays especial chars correctly and not like a upside down question mark...

推荐答案

这似乎并不明显,但是为了绕过IIS错误,你需要做的是这样的:

It didn't seem obvious, but in order to bypass the IIS errors, you need to do something like:

<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Auto">
    <remove statusCode="403" />
  </httpErrors>
</system.webServer>

从本质上讲,你需要重写/删除默认的处理程序错误code你回来(在这种情况下,禁止/ 403)。否则,你的403响应会触发IIS的错误处理,你会得到:

Essentially, you need to override/remove the default handler for the error code you're returning (in this case, Forbidden/403). Otherwise, your 403 response will trigger IIS's error handling and you get:

IIS通用禁页

这是不言自明的,我想。

which is self-explanatory, I think.

这确实引入了另一个问题:这是站点范围。它的可能的可以处理在每个动作的基础响应code(可能带有过滤器,尽管这可能是太远进入管道),但我不知道在这点。

This does introduce another problem: this is site-wide. It might be possible to handle the response code on a per-action basis (possibly with a filter, though that may be too far into the pipeline), but I'm not sure at this point.

这篇关于ASP.NET MVC返回超过预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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