ASP. net mvc custom processing JSON ActionResult class, jsonactionresult

Source: Internet
Author: User

ASP. net mvc custom processing JSON ActionResult class, jsonactionresult

1. Unify the JSON format and specify the ContentType to solve the problem of downloading when the ContentType is set to application/json when the browser of a lower version obtains the json format.

 public abstract class CustomResult<TData> : ActionResult    {        public abstract TData GetObject();        protected JsonSerializerSettings SerializerSettings;        protected void InitSerialization(ControllerContext context)        {            HttpResponseBase response = context.HttpContext.Response;            response.ContentType = "text/html";            TData data = GetObject();            if (SerializerSettings == null)            {                SetSerializerSettings();            }            response.Write(JsonConvert.SerializeObject(data, Formatting.None, SerializerSettings));        }        protected virtual void SetSerializerSettings()        {            SerializerSettings = new JsonSerializerSettings            {                              Converters = new List<JsonConverter>                {                    new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd hh:mm" }                }            };        }        public override void ExecuteResult(ControllerContext context)        {            InitSerialization(context);        }    }

 

  


In aspnet MVC, no actionResult is written in the controller. What is the difference between writing an application? What can be done?

By default, the controller in mvc returns an actionresult (action, result). You can also return htmlresult, jsonresult, view, And, which are subclasses of actionresult. First, it is a return type. The difference between the global application object written to the application can be distinguished from the next layer. Or I didn't understand what it meant to you, and asked if you had any questions.

In the Controller of Aspnet MVC, what should I do when the JsonResult method of the returned type needs to jump to other actions?

Change public JsonResult Exchange to public ActionResult Exchange.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.