HTTP 失败响应会覆盖非本地客户端的响应正文

发布于 2024-11-08 07:04:51 字数 1817 浏览 0 评论 0原文

我正在为某些公共页面创建一种锁定机制,该机制只允许一个用户同时编辑它们。客户端向服务器发出 GET API 调用来请求锁定,服务器返回一个 MVC ActionResult 对象。由于某种原因,当我在本地运行程序并返回 new HttpConflictResult("Cannot acquire lock", message); (表明获取锁失败)时,它工作正常。错误消息被正确发送和接收,并且可以显示在警报或任何需要的内容中。但是,当我远程访问该站点时,该消息被“由于存在冲突而未显示页面”覆盖。我做错了什么或者这是 IIS 的“功能”还是什么?如果是,有没有办法解决这个问题,以便我仍然可以向客户发送我想要的消息?

预先感谢您的任何帮助!

编辑:抱歉,忘记了 HttpConflictResult 是一个继承类。以下是一些相关信息:

public class HttpConflictResult : HttpErrorResult
{
    public HttpConflictResult() : this(string.Empty) { }
    public HttpConflictResult(string errorMessageResource) : this("Conflict", errorMessageResource, true) { }
    public HttpConflictResult(string statusMessage, string errorMessage) :
        base(409, statusMessage, errorMessage) { }
}

public class HttpErrorResult : ActionResult
{
    protected int _statusCode;
    protected string _statusMessage;
    protected string _errorMessage;

    public HttpErrorResult(int statusCode, string statusMessage, string errorMessageResource)
    {
        _statusCode = statusCode;
        _statusMessage = statusMessage;
        _errorMessage = errorMessage;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.StatusCode = _statusCode;
        context.HttpContext.Response.StatusDescription = _statusMessage;
        if(_errorMessage != null)
        {
            context.HttpContext.Response.ContentType = "application/json; charset=utf-8";
            context.HttpContext.Response.Clear();

            System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            context.HttpContext.Response.Write(ser.Serialize(new { message = _errorMessage }));
        }
    }
}

如果我遗漏了其他内容,请告诉我。再次感谢!

I'm creating a locking mechanism for some public pages that should only allow one user to edit them at the same time. The client makes GET API calls to the server requesting a lock, which returns an MVC ActionResult object. For some reason, when I'm running the program locally and I return a new HttpConflictResult("Cannot acquire lock", message); (indicating that acquiring the lock failed), it works fine. The error message is sent and received properly and can be displayed in an alert or whatever is needed. However, when I'm accessing the site remotely, the message is overwritten with "The page was not displayed because there was a conflict." Am I doing something wrong or is this a 'Feature' of IIS or something? If it is, is there a way around it so that I can still get the message that I want to the client?

Thanks in advance for any help!

EDIT: Sorry, forgot that HttpConflictResult was an inherited class. Here's some info on it:

public class HttpConflictResult : HttpErrorResult
{
    public HttpConflictResult() : this(string.Empty) { }
    public HttpConflictResult(string errorMessageResource) : this("Conflict", errorMessageResource, true) { }
    public HttpConflictResult(string statusMessage, string errorMessage) :
        base(409, statusMessage, errorMessage) { }
}

public class HttpErrorResult : ActionResult
{
    protected int _statusCode;
    protected string _statusMessage;
    protected string _errorMessage;

    public HttpErrorResult(int statusCode, string statusMessage, string errorMessageResource)
    {
        _statusCode = statusCode;
        _statusMessage = statusMessage;
        _errorMessage = errorMessage;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.StatusCode = _statusCode;
        context.HttpContext.Response.StatusDescription = _statusMessage;
        if(_errorMessage != null)
        {
            context.HttpContext.Response.ContentType = "application/json; charset=utf-8";
            context.HttpContext.Response.Clear();

            System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            context.HttpContext.Response.Write(ser.Serialize(new { message = _errorMessage }));
        }
    }
}

Let me know if I left anything else out. Thanks again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

錯遇了你 2024-11-15 07:04:51

如果您正在设置错误代码(即 4xx 或 5xx 范围内的 http 状态代码),并且您在 IIS 7 下运行,那么您需要确保已告诉 IIS 不要使用标准错误消息。

您可以使用 TrySkipIisCustomErrors 来执行此操作Response 对象上的 参数。尝试按如下方式修改 HttpErrorResult:

public override void ExecuteResult(ControllerContext context)
{
    // Set TrySkipIisCustomErrors to ensure ASP.NET sends your error content to the
    // user instead of the default ASP.NET content under IIS 7.
    context.HttpContext.Response.TrySkipIisCustomErrors = true;

    context.HttpContext.Response.StatusCode = _statusCode;
    context.HttpContext.Response.StatusDescription = _statusMessage;

    if(_errorMessage != null)
    {
        [...]
    }
}

If you're setting error codes (i.e. http status codes in the 4xx or 5xx ranges), and you're running under IIS 7, then you need to ensure you've told IIS not to use the standard error messages.

You do this with the TrySkipIisCustomErrors parameter on the Response object. Try modifying your HttpErrorResult as follows:

public override void ExecuteResult(ControllerContext context)
{
    // Set TrySkipIisCustomErrors to ensure ASP.NET sends your error content to the
    // user instead of the default ASP.NET content under IIS 7.
    context.HttpContext.Response.TrySkipIisCustomErrors = true;

    context.HttpContext.Response.StatusCode = _statusCode;
    context.HttpContext.Response.StatusDescription = _statusMessage;

    if(_errorMessage != null)
    {
        [...]
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文