mvc5-3 Result Analysis

Source: Internet
Author: User

A multitude of result

Using MVC for development, you can see ActionResult, Contentreuslt, Jsonresult. And so on, today the results are analyzed behind the scenes. How it's done ActionResult

In development we all encounter an interesting phenomenon, using ActionResult to do the return type, we can return json,view,content and other result, why does it do so?

Analyze what we can do with it. Some people may have guessed, abstract classes and interfaces. The definition rule for an interface is I begins with. Then there is no. There is no doubt that the abstract class. As you can see, ActionResult is actually an abstract class in which there is an abstract method Executeresult. There is no doubt that the return type ActionResult can use JSON, Content, view, and so on. Because they all inherit the ActionResult and implement the Executeresult Method!

The commonly used result principle analytic jsonresult

Jsonresult is a very common result, and the direct JSON (STR) approach is very simple.

You can visually see that Jsonresult inherits from ActionResult. This confirms the speculation between us. The Executeresult method is implemented. As you can see, you get the HttpContext context object. Then response the conntype= "Application/json", using JavaScriptSerializer to serialize and then return.

It's so simple. JSON also has a jsonrequestbehavior parameter that sets the security of this method's data. By default, the GET request is filtered out, and you can see that the constructor is assigned. We can also manually change

Contentresult

Direct content return data is also very common in MVC, with the jsonresult principle to see the content parameters. It's time to think about how it's handled.

Compared to Jsonresult, it is much simpler. Because only the text is returned, the parameter has text content and returns the text type and encoding format. Call Response.Write There's nothing much to say

Javascriptresult

Javascriptresult looks cool because we can directly return to JavaScript code, which makes a lot of small partners excited to see what's behind it.

Very simply, assign the Response.ContentType to "Application/x-javascript" and write.

Fileresult

Everyone should have guessed how it was dealt with in the background. Isn't it WriteFile?

Yes, it is, indeed, WriteFile. There are six overloads so the code is more, but there is no magic place, I believe we can understand

mvc5-3 Result Analysis

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.