Asp.net 5 web api Custom ActionResult



这是我的实现自定义ActionResult

public class ResultData<T>
{
public string Code { get; set; }
public string Massage { get; set; }
public T Result { get; set; }
}
public class BaseApiResult<T> : JsonResult
{
public BaseApiResult() : base(null)
{
ContentType = "application/json";
}
public IActionResult Success(T result)
{
Value = new ResultData<T>
{
Code = "2000",
Massage = "Sucsess",
Result = result
};
StatusCode = 200;

return this;
}
public IActionResult Fail(T result, string code, string massage)
{
Value = new ResultData<T>
{
Code = code,
Massage = massage,
Result = result
};
StatusCode = 400;
return this;
}
public IActionResult ServerError(T result)
{
Value = new ResultData<T>
{
Code = "5000",
Massage = "Internal Server Error",
Result = result
};
StatusCode = 500;

return this;
}
}

这就是我们怎么称呼它的

[HttpGet("GetProducts")]
public IActionResult GetProducts()
{
try
{
var res = _entityFrameworkCoreService.GetAllProducts();
if (res.Count <= 0)
{
return new BaseApiResult<string>().Fail(null, "4002", "NoRecord found");
}
return new BaseApiResult<IList<DtoProSubCat>>().Sucsees(res); ;
}
catch (Exception ex)
{
return new BaseApiResult<string>().ServerError(ex.Message);
}
}

现在我要问的是:-

1-这是ActionResult的错误实现吗?

2-这个实现有性能问题吗?

3-有什么增强添加到这个实现吗?

3-实现自定义ActionResult的最佳实践是什么?

上面的方法仍然可以正常工作,但我个人认为它不好。首先,您真正需要的是返回包装在ResultData中的结果。这被认为是一个数据模型,而不是一种行为。应该知道IActionResult不仅仅是返回数据,而且在Asp MVC操作模型中,它也是HTTP服务器的输出处理程序。

我提出以下几个解决方案,我相信它们会更合适。

方法1:为ControllerBase创建基类。如:

public class ControllerBase : Microsoft.AspNetCore.Mvc.Controller
{
protected IActionResult MyResult <T> (int statusCode, ResultData<T> result)
{
var jsonResult = Json(result);
jsonResult.StatusCode = statusCode;
return jsonResult;
}
protected IActionResult Success <T> (T result)
{
return MyResult(200, new ResultData<T>
{
Code = "2000",
Massage = "Sucsess",
Result = result
});
}
protected IActionResult Fail <T> (T result, string code, string massage)
{
return MyResult(400, new ResultData<T>
{
Code = code,
Massage = massage,
Result = result
});
}
protected IActionResult ServerError<T>(T result)
{
return MyResult(500, new ResultData<T>
{
Code = "5000",
Massage = "Internal Server Error",
Result = result
});
}
}
/** implement */
public class HomeController : ControllerBase
{
[HttpGet("GetProducts")]
public IActionResult GetProducts()
{
try
{
IList<DtoProSubCat> res = new List<DtoProSubCat>() { new DtoProSubCat() }; //
if (res.Count <= 0)
{
return Fail<string>(null, "4002", "NoRecord found");
}
return Success<IList<DtoProSubCat>>(res); ;
}
catch (Exception ex)
{
return ServerError(ex.Message);
}
}
}

一个类似的方法是为Controller创建一个扩展函数,或者创建一个包含处理数据的函数的静态类。

方法2:为每个响应行为创建一个特定的类。如:

public class SuccessResult <T> : JsonResult
{
public SuccessResult (T result)
: base(new ResultData<T>
{
Code = "2000",
Massage = "Sucsess",
Result = result
})
{
StatusCode = 200;
}
}
public class FailResult<T> : JsonResult
{
public FailResult(T result, string code, string massage)
: base(new ResultData<T>
{
Code = code,
Massage = massage,
Result = result
})
{
StatusCode = 400;
}
}
public class ServerErrorResult<T> : JsonResult
{
public ServerErrorResult(T result)
: base(new ResultData<T>
{
Code = "5000",
Massage = "Internal Server Error",
Result = result
})
{
StatusCode = 500;
}
}
/** implement */
public class HomeController : Controller
{
[HttpGet("GetProducts")]
public IActionResult GetProducts()
{
try
{
IList<DtoProSubCat> res = new List<DtoProSubCat>() { new DtoProSubCat() }; //
if (res.Count <= 0)
{
return new FailResult<string>(null, "4002", "NoRecord found");
}
return new SuccessResult<IList<DtoProSubCat>>(res); ;
}
catch (Exception ex)
{
return new ServerErrorResult<string>(ex.Message);
}
}
}

您实现上述命令的方式使得几行命令毫无意义,当然在应用程序运行期间没有太大问题,但从长远来看会影响扩展或维护过程。以上两种方法可以帮助您保护函数的运行模型,最小化源代码,并最小化无意义的命令行。

这个V2怎么样@Henry Trần给了一个提示

public class BaseApiResultV2
{
private IActionResult SetResult<T>(int statusCode, ResultData<T> result)
{
JsonResult jsonResult = new(result)
{
StatusCode = statusCode,
ContentType = "application/json"
};
return jsonResult;
}
public IActionResult Success<T>(T result)
{
return SetResult(StatusCodes.Status200OK,
new ResultData<T>
{
Code = "2000",
Massage = "Sucsess",
Result = result
});
}
public IActionResult Fail<T>(T result, string code, string massage)
{
return SetResult(StatusCodes.Status400BadRequest,
new ResultData<T>
{
Code = code,
Massage = massage,
Result = result
});
}
public IActionResult ServerError<T>(T result)
{
return SetResult(StatusCodes.Status500InternalServerError,
new ResultData<T>
{
Code = "5000",
Massage = "Internal Server Error",
Result = result
});
}
private struct ResultData<T>
{
public string Code { get; set; }
public string Massage { get; set; }
public T Result { get; set; }
}
}

相关内容

  • 没有找到相关文章

最新更新