【发布时间】:2016-03-29 19:33:06
【问题描述】:
对于 ASP.NET Web API 2 服务甚至是独立的 MVC 系统,由于我处理错误的方式等原因,我更喜欢在我的控制器中使用 JsonResult 函数:
public class BaseController : Controller
{
// This controller is where functionality common to all
// controllers (such as error reporting goes. It's also good for avoiding
// code repetition as in the case of the next function
public JsonResult CreateResponse(object Data)
{
// send a JsonResult with the specified data
return Json(Data, JsonRequestBehavior.AllowGet);
}
}
public class UserController : BaseController
{
public JsonResult Create(CreateUserViewModel Model)
{
try
{
var User = new User
{
Username = Model.Username,
EmailAddress = Model.EmailAddress,
Password = Hashing.CreateHash(Model.Password)
};
db.Users.Add(User);
db.SaveChanges();
return CreateResponse(true);
}
catch (Exception ex)
{
return CreateResponse(ex.Message);
}
}
}
在什么情况下我希望对 JsonRequestBehavior 进行 AllowGet 或 DenyGet?
这两门课程的含义或顾虑是什么?
【问题讨论】:
-
有人投票 可能的答案太多,或者对于这种格式来说,好的答案太长了。请添加详细信息以缩小答案范围或隔离可以在几段中回答的问题。我当然不同意。
-
@ErikPhilips 如何在不知道可能有什么答案的情况下做到这一点?如果我确实知道哪些答案适用于这里,那么提问不是浪费时间吗?没有意义
-
@Ortund 这不是关于你,而是关于你的问题。想象一下有人问,我应该使用 c# 还是 java 来做 blah
标签: c# asp.net json asp.net-mvc