【发布时间】:2017-02-27 08:12:19
【问题描述】:
【问题讨论】:
标签: c# .net asp.net-mvc
【问题讨论】:
标签: c# .net asp.net-mvc
是的,只需使用如果您想更改从服务器返回的 Josn 响应的结构,您可以使用 asp.net mvc app 中的以下代码创建新的响应。
// here you can use your own properties which then can be send to client .
return Json(new { Status= false ,Description = response.Message });
如果你有控制器方法,那么你应该返回 JsonResult
如果您正在寻找通用解决方案,请查看这篇文章,它可能会对您有所帮助。
【讨论】:
可以使用自定义 AuthorizeAttribute 来完成。
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public CustomAuthorizeAttribute ()
{
}
public override void OnAuthorization(HttpActionContext actionContext)
{
try
{
if (Authorize(actionContext))
{
return;
}
HandleUnauthorizedRequest(actionContext);
}
catch (Exception)
{
//create custom response
actionContext.Response = actionContext.Request.CreateResponse(
HttpStatusCode.OK,
customresponse
);
return;
}
}
protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
{
//create custom unauthorized response
actionContext.Response = actionContext.Request.CreateResponse(
HttpStatusCode.OK,
customunauthorizedresponse
);
return;
}
private bool Authorize(HttpActionContext actionContext)
{
//authorization logics
}
}
在您的 api 控制器方法中,您可以使用 [CustomAuthorizeAttribute] insted of [Authorize]
【讨论】: