![](/img/trans.png)
[英]JsonRequestBehavior equivalent in Json.Net with Asp.Net Mvc
[英]ASP.NET MVC - JSON.NET extension method for controller
我剛剛將JSON.NET添加到我的項目中,我想創建一個名為JsonNet的擴展方法,其行為與Json方法相同,但使用的是JSON.NET。
我在這里有一個使用JSON.NET擴展JsonResult的類:
public class JsonNetResult : JsonResult {
public override void ExecuteResult(ControllerContext context) {
if (context == null)
throw new ArgumentNullException("context");
var response = context.HttpContext.Response;
response.ContentType = !String.IsNullOrEmpty(ContentType)
? ContentType
: "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
var serializedObject = JsonConvert.SerializeObject(Data, Formatting.Indented);
response.Write(serializedObject);
}
}
在ExecuteResult方法下面,我嘗試添加以下內容:
public static JsonNetResult JsonNet(this Controller controller, object data) {
var result = new JsonNetResult();
result.Data = data;
return result;
}
public static JsonNetResult JsonNet(this Controller controller, object data, JsonRequestBehavior behavior) {
var result = new JsonNetResult();
result.Data = data;
result.JsonRequestBehavior = behavior;
return result;
}
然后我有我的控制器:
public class SomethingController : Controller {
public ActionResult SomeAction() {
object data = SomeClass.GetData();
return JsonNet(data, JsonRequestBehavior.AllowGet);
}
}
並且編譯器找不到JsonNet方法。 即使我嘗試這個:
return ((Controller)this).JsonNet(data, JsonRequestBehavior.AllowGet);
它仍然無法正常工作。
如果我將JsonNet中的代碼復制到SomeAction中,它可以正常工作,所以我知道SomethingController可以看到JsonNetResult。
如果有幫助,這兩個類位於不同的名稱空間中。
這是我現在使用的那個,它也對我從中提取一些來源的頁面有評論。 我已經對自己進行了調整,包括擴展方法。
如上所述,您需要在控制器中添加適當的using命名空間。
我只是使用return this.JsonNet(data)
來調用它。 我只是允許自定義格式化的覆蓋,還有一個因為我遇到了需要內容類型為“普通/文本”的JS插件。
//http://james.newtonking.com/archive/2008/10/16/asp-net-mvc-and-json-net.aspx
public class JsonNetResult : ActionResult
{
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public JsonSerializerSettings SerializerSettings { get; set; }
public Formatting Formatting { get; set; }
public JsonNetResult()
{
SerializerSettings = new JsonSerializerSettings
{
//http://odetocode.com/blogs/scott/archive/2013/03/25/asp-net-webapi-tip-3-camelcasing-json.aspx
#if DEBUG
Formatting = Formatting.Indented, //Makes the outputted Json for structures for easier reading by a human, only needed in debug
#endif
ContractResolver = new CamelCasePropertyNamesContractResolver() //Makes the default for properties outputted by Json to use camelCaps
};
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType)
? ContentType
: "application/json";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data != null)
{
JsonTextWriter writer = new JsonTextWriter(response.Output) {Formatting = Formatting};
JsonSerializer serializer = JsonSerializer.Create(SerializerSettings);
serializer.Serialize(writer, Data);
writer.Flush();
}
}
}
public static class JsonNetExtenionMethods
{
public static ActionResult JsonNet(this Controller controller, object data)
{
return new JsonNetResult() {Data = data};
}
public static ActionResult JsonNet(this Controller controller, object data, string contentType)
{
return new JsonNetResult() { Data = data, ContentType = contentType };
}
public static ActionResult JsonNet(this Controller controller, object data, Formatting formatting)
{
return new JsonNetResult() {Data = data, Formatting = formatting};
}
}
除非它們在靜態類中,否則編譯器將找不到擴展方法。 嘗試將JsonNet
方法放在他們自己的靜態類中,例如JsonNetExtensions
。 還要確保擴展類與控制器位於同一名稱空間中,或者控制器在頂部使用擴展類名稱空間的using
語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.