自定义一个继承AuthorizeAttribute的类,代码如下:
public class RequireAuthorizeAttribute : AuthorizeAttribute { public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext) { if (actionContext.Request.Method == HttpMethod.Post) { HttpContent content = actionContext.Request.Content;
//执行该句,会将action的参数实体置为空 Task<JObject> task = content.ReadAsAsync<JObject>(); var result = task.Result; } } }
Action如下:
public class TestController : ApiController { [RequireAuthorize] //加上该过滤器,参数info变成了null [HttpPost] public string GetResult(UserInfo info) { return info.Name; } } [Serializable] public class UserInfo { public int ID { get; set; } public string Name { get; set; } }
前台调用:
$("#testbtn").click(function () { var parm = { ID: 1, Name: "Jone" }; $.post("api/test/GetResult", parm, function (data) { alert(data); }, "text"); })
为什么会出现这种问题呢,该怎么避免呢