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