c# - MVC Controller 操作返回字符串的替代方案?

标签 c# ajax asp.net-mvc-4 knockout.js

我一直在关注使用 Knockout 和 Entity Framework 的 MVC 项目的在线教程。我也在使用存储库模式。

根据我的发现,教程在执行 HTTP POST 请求时返回字符串,如下所示。我担心的是 Controller 正在返回字符串——它看起来非常简陋,而且我似乎找不到可靠的示例/教程来说明我如何以其他方式执行这些 POST 请求(同时不返回 View ),以及捕获数据库异常和 POST 异常也是如此。

Javascript:

self.saveNRI = function () {
        var token = $('[name=__RequestVerificationToken]').val();
        var headers = {};
        headers["__RequestVerificationToken"] = token;
        $.ajax({
            type: "POST",
            url: '/nris/Create',
            headers: headers,
            dataType: 'json',
            data: ko.toJSON(self.nri),
            contentType: 'application/json',
            success: function (result) { },
            error: function (err) {
                if (err.responseText == "Success") {
                    window.location.href = '/nris/Index/';
                }
                if (err.responseText == "Failed") {
                    alert("save failed");
                }
            }
        });
    }

Controller :

[HttpPost]
public string Create(DTO incomingModel)
{  
    if (ModelState.IsValid){
        try
        {
            _nriRepository.Insert(incomingModel);                    
        }
        catch (Exception ex)
        {
            return "Failed";
        }         
    }
    return "Success";
}

存储库:

public async void Insert(DTO n)
{
    //Insert code removed for brevity
        await _context.SaveChangesAsync();
}

最佳答案

使用旧的正则可能是个好主意 HTTP Status Codes在您的 API 上传达操作结果。

创建资源时表示成功的适当代码是 201。通常,响应正文将包含新创建资源的 ID 或 URL。

10.2.2 201 Created

The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field. The response SHOULD include an entity containing a list of resource characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. The origin server MUST create the resource before returning the 201 status code. If the action cannot be carried out immediately, the server SHOULD respond with 202 (Accepted) response instead.

但是,您也可以使用更通用的状态代码 200 来指示成功:

10.2.1 200 OK

The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:

GET an entity corresponding to the requested resource is sent in the response;

HEAD the entity-header fields corresponding to the requested resource are sent in the response without any message-body;

POST an entity describing or containing the result of the action;

TRACE an entity containing the request message as received by the end server.

要传达失败信息,您可能会使用 4xx5xx 范围内的代码。

4xx 表示故障在客户端。例如,客户端没有发送创建资源所需的所有属性。通用的 400 很方便:

10.4.1 400 Bad Request

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

但是,5xx 表示您的服务器在完成请求时遇到了问题。

10.5.4 503 Service Unavailable

The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header. If no Retry-After is given, the client SHOULD handle the response as it would for a 500 response.

在客户端,您可以依靠 jQuery.ajax 来根据状态代码适本地路由您的回调。您应该使用更新的基于 Promise 的 API 而不是成功和错误回调。例如:

var jqxhr = $.ajax( "example.php" )
  .done(function() {
    alert( "success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });

来源:http://api.jquery.com/jquery.ajax/

您可以使用优秀的 httpstat.us Web 服务来测试响应代码。只需将所需的响应代码附加到 URL,它就会返回一个模拟答案。例如成功:http://httpstat.us/200

关于c# - MVC Controller 操作返回字符串的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27951442/

相关文章:

c# - 使用配置文件指定连接字符串来定义单元测试中的数据源

c# - Simple Injector - 将接口(interface)的特定实现注入(inject)到特定 Controller

javascript - 在php中执行一个php函数

asp.net - ViewBag 名称可以与 DropDownList 中的 Model 属性名称相同吗?

c# - 使用 Web Api 创建格式不正确的 Json 格式

php - Codeigniter 通过 Ajax/Jquery 分页问题

javascript - Ajax 重置部分选择框,但不是全部

javascript - 如何将 JSON.NET 日期时间格式转换为 JavaScript 日期和时间

javascript - 强制用户使用 jquery datepicker

c# - 如何防止提交后清除输入的密码?