从 JQuery AJAX GET 获取 HttpContext

发布于 2024-08-03 15:35:54 字数 900 浏览 11 评论 0原文

我的项目使用 Castle Monorail 作为 MVC 框架。问题是单轨铁路要求所有控制器都有一个视图模板。我正在尝试通过 AJAX 调用从控制器上的方法获取字符串。 AJAX GET 始终从服务器返回 500 错误,因为 Monorail 找不到视图模板。我见过其他解决方法的示例,其中您只需将控制器方法的返回类型设置为 void(这表明单轨列车不必费心寻找视图控制器),然后执行以下操作:

Context.Response.OutputStream.Write(buffer, 0, buffer.Length);

将上下文写入屏幕。

所以我有 follow 方法,并尝试通过 Jquery AJAX GET 获取返回字符串。有人可以帮忙吗?

 public void Note(string id)
    {
        if (!string.IsNullOrEmpty(id))
        {
            if (notesProvider.HasNote(id))
            {
                return "{status:'200', text: '" + notesProvider.GetNote(id).Body + "'}";

            }
            else return "{status:'404', text: 'Could not find the Note by provided id [" + id + "]'}";
        }
        else return "{status:'500', text: 'Illegal request : a note id must be provided'}";
    }
}

我应该如何使此返回无效并通过 HTTPCOntext 读取返回值?

My project is using Castle Monorail as a MVC framework. The problem is that Monorail requires that all controllers have a view template. I am trying to get a string back from a method on a controller via an AJAX call. The AJAX GET always returns a 500 error from the server because Monorail cannot find a view template. I have seen other examples of workarounds in which you just set the return type of the controller method to void (this signals monorail to not bother finding a view controller) and then doing something like:

Context.Response.OutputStream.Write(buffer, 0, buffer.Length);

To just write the context to the screen.

So I have the follow method and am trying to get the return string via a Jquery AJAX GET. Can someone help?

 public void Note(string id)
    {
        if (!string.IsNullOrEmpty(id))
        {
            if (notesProvider.HasNote(id))
            {
                return "{status:'200', text: '" + notesProvider.GetNote(id).Body + "'}";

            }
            else return "{status:'404', text: 'Could not find the Note by provided id [" + id + "]'}";
        }
        else return "{status:'500', text: 'Illegal request : a note id must be provided'}";
    }
}

How should I make this return void and read the return values via the HTTPCOntext?

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

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

发布评论

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

评论(2

鸠魁 2024-08-10 15:35:54

所有派生的单轨控制器都可以访问名为“CancelView()”的方法。在您希望返回的方法中执行此方法,而不使用视图模板。

All derived Monorail Controllers have access to a method called, "CancelView()". Execute this method within the method that you wish to return without using a view template.

ヤ经典坏疍 2024-08-10 15:35:54

您可以使用 JsonReturnBinder 来实现:

[return:JSonReturnBinder]
public object Note(string id)
{
    if (!string.IsNullOrEmpty(id))
    {
        if (notesProvider.HasNote(id))
        {
            return new {status=200, text= notesProvider.GetNote(id).Body };
        }
        else return new {status=404, text="Could not find the Note by provided id [" + id + "]" };
    }
    else return new {status =500, text="Illegal request : a note id must be provided" };
}

优化建议:

为什么将状态代码作为响应正文的一部分返回?任何响应中都有 HttpStatusCode 字段。将其设置为所需的代码将使客户端代码更易于使用(您可以直接检查 XMLHttpRequest 中的状态代码,并且大多数 JS 库都有专门的成功和错误处理程序),而且您的方法将返回所需的字符串,因此单元测试会容易得多。示例:

[return:JSonReturnBinder]
public string Note(string id)
{
    if (!string.IsNullOrEmpty(id))
    {
        if (notesProvider.HasNote(id))
        {
            return notesProvider.GetNote(id).Body;
        }
        else 
        {
            Response.StatusCode = 404;
            return "Could not find the Note by provided id [" + id + "]";
        }
    }
    else 
    {
        Response.StatusCode = 500;
        return "Illegal request : a note id must be provided";
    }
}

另一个建议 - 打乱 if/else 块以减少嵌套级别:

[return:JSonReturnBinder]
public string Note(string id)
{
    if (string.IsNullOrEmpty(id))
    {
        Response.StatusCode = 500;
        return "Illegal request : a note id must be provided";
    }

    if (notesProvider.HasNote(id) == false)
    {
        Response.StatusCode = 404;
        return "Could not find the Note by provided id [" + id + "]";
    }

    return notesProvider.GetNote(id).Body;
}

这样代码以防护措施(前提条件)开始,方法中的最后一个 return 语句代表正常执行完成。 IMO 它使事情更容易阅读。

You can use the JsonReturnBinder for that:

[return:JSonReturnBinder]
public object Note(string id)
{
    if (!string.IsNullOrEmpty(id))
    {
        if (notesProvider.HasNote(id))
        {
            return new {status=200, text= notesProvider.GetNote(id).Body };
        }
        else return new {status=404, text="Could not find the Note by provided id [" + id + "]" };
    }
    else return new {status =500, text="Illegal request : a note id must be provided" };
}

Refinement suggestion:

Why return the status code as part of the response body? there is the HttpStatusCode field in any response. Setting it to the desired code will make client code easier to use (you can inspect the status code in the XMLHttpRequest directly, and most JS libraries has dedicated handlers for success and errors), plus your method will return the desired string, so Unit Testing would be much easier. Example:

[return:JSonReturnBinder]
public string Note(string id)
{
    if (!string.IsNullOrEmpty(id))
    {
        if (notesProvider.HasNote(id))
        {
            return notesProvider.GetNote(id).Body;
        }
        else 
        {
            Response.StatusCode = 404;
            return "Could not find the Note by provided id [" + id + "]";
        }
    }
    else 
    {
        Response.StatusCode = 500;
        return "Illegal request : a note id must be provided";
    }
}

Another suggestion - shuffle the if/else blocks to reduce nesting levels:

[return:JSonReturnBinder]
public string Note(string id)
{
    if (string.IsNullOrEmpty(id))
    {
        Response.StatusCode = 500;
        return "Illegal request : a note id must be provided";
    }

    if (notesProvider.HasNote(id) == false)
    {
        Response.StatusCode = 404;
        return "Could not find the Note by provided id [" + id + "]";
    }

    return notesProvider.GetNote(id).Body;
}

This way the code begins with safeguards (pre-conditions), and the last return statement in the method represents the normal execution completion. IMO it makes things easier to read.

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