如何在基本控制器中覆盖Json helper方法的行为

时间:2017-01-29 06:13:12

标签: c# json ajax asp.net-mvc jsonresult

我有以下问题:

  

您的应用程序将以JSON格式响应AJAX请求。在   为了最大限度地控制序列化,你将实现一个   自定义ActionResult类。

     

您必须覆盖基础中Json帮助程序方法的行为   控制器,以便所有JSON响应将使用自定义结果   类。你应该继承哪一堂课?

响应类型为 JsonResult 。代码方面我很难看到结构。当我读到" 实施"在这个问题中,我想到了一个接口,所以这就是我想出来的:

public class CustAction:ActionResult
{
  //max control over serialization
}
public interface ICustAction:CustAction
{
}

public controller MyController:ICustAction, JsonResult
{
   //override Json() method in here
}

上述代码是否适用于上述问题?

2 个答案:

答案 0 :(得分:4)

您可以覆盖 JsonResult ,并返回自定义JsonResult。 例如,

StandardJsonResult

public abstract class BaseController : Controller
{
    protected StandardJsonResult JsonValidationError()
    {
        var result = new StandardJsonResult();

        foreach (var validationError in ModelState.Values.SelectMany(v => v.Errors))
        {
            result.AddError(validationError.ErrorMessage);
        }
        return result;
    }

    protected StandardJsonResult JsonError(string errorMessage)
    {
        var result = new StandardJsonResult();

        result.AddError(errorMessage);

        return result;
    }

    protected StandardJsonResult<T> JsonSuccess<T>(T data)
    {
        return new StandardJsonResult<T> { Data = data };
    }
}

基本控制器

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        return JsonResult(null, JsonRequestBehavior.AllowGet)

        // Uncomment each segment to test those feature.

        /* --- JsonValidationError Result ---
            {
                "success": false,
                "originalData": null,
                "errorMessage": "Model state error test 1.\nModel state error test 2.",
                "errorMessages": ["Model state error test 1.", "Model state error test 2."]
            }
            */
        ModelState.AddModelError("", "Model state error test 1.");
        ModelState.AddModelError("", "Model state error test 2.");
        return JsonValidationError();

        /* --- JsonError Result ---
            {
                "success": false,
                "originalData": null,
                "errorMessage": "Json error Test.",
                "errorMessages": ["Json error Test."]
            }
        */
        //return JsonError("Json error Test.");

        /* --- JsonSuccess Result ---
            {
                "firstName": "John",
                "lastName": "Doe"
            }
        */
        // return JsonSuccess(new { FirstName = "John", LastName = "Doe"});
    }
}

用法

{{1}}

信用:Building Strongly-typed AngularJS Apps with ASP.NET MVC 5 by Matt Honeycutt

答案 1 :(得分:1)

public class customJsonResult : JsonResult
{
  //max control over serialization
 }

//in the base controller override the Controller.Json helper method:
protected internal override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new customJsonResult {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding,
        JsonRequestBehavior = behavior
    };
}