Controller-various actionresult
All actions we see are return view (); we can think of this return value to parse An ASPX file. And its return type is actionresult, such
public ActionResult Index() { return View(); }
In addition to view (), what values can we return here?
<1> ascx page
Scenario: return code snippets. For example, Ajax returns a subpage.
Let's first create a testcontroller. CS Controller; first create an action
public ActionResult Ascx() { return PartialView(); }
Then add a view ascx. cshtml to this action.
@ {Layout = NULL ;}<! Doctype HTML> Run http: // localhost: 8439/test/ascx in the browser to obtain an ascx page.
<2> return text
Add an action method under the test Controller
Public actionresult text () {return content ("this is a piece of text ");}
You do not need to add this view.
Directly run http: // localhost: 8439/test/text in the browser. The result is that the line "this is a piece of text" is output on the webpage.
<3> return JSON
Sometimes, when Ajax is called, the returned object is required to be serialized in JSON format, for example:
Public actionresult showjson () {var userinfo = new {name = "Obama", age = 56}; return JSON (userinfo, jsonrequestbehavior. allowget); // var tempobj = new {controller = "democontroller", Action = "jsonresultdemo"}; // return JSON (tempobj, jsonrequestbehavior. allowget); // jsonrequestbehavior. allowget: allows http get requests from clients // return JSON (user, jsonrequestbehavior. allowget );}
Run http: // localhost: 8439/test/showjson in the browser and a form is displayed to save the showjson. JSON file.
After saving the image, open the file in Notepad. The file content is: {"name": "Obama", "Age": 56}
<4> output JS files
Most JS files are static files, but sometimes JS files may need to be dynamically generated. In this case, we can output these files as follows:
public ActionResult Js() { return JavaScript("var x=0;"); }
Run: http: // localhost: 8439/test/Js in the browser to bring up a form:
After saving, open the Js. js file in notepad with the following content: var x = 0;
<5> page Jump 1. Jump to action
Public actionresult rdaction () {return redirecttoaction ("text", "test"); // jump to the text method under the control of test}
Run: http: // localhost: 8439/test/rdaction in the browser and jump to the http: // localhost: 8439/test/text page immediately.
2. Jump to URL
public ActionResult rdurl() { return Redirect("http://www.baidu.com"); }
Run: http: // localhost: 8439/test/rdurl in the browser and immediately jump to the Baidu homepage.
3. Jump to the routing rule
Public actionresult rdrouting () // jump to the routing rule {return redirecttoroute ("default", // route name New {controller = "test", Action = "ascx "});}
Run: http: // localhost: 8439/test/rdrouting in the browser and immediately jump to the ascx method under the test controller, that is, jump to http: // localhost: 8439/test/ascx page
Therefore, the content of the http: // localhost: 8439/test/ascx page is output.
<6> display files
public ActionResult fn() { return File("d:/123.jpg","jpg/png/JPEG"); }
Run: http: // localhost: 8439/test/FN in the browser to bring up a dialog box:
Click "save" and select "open with Image Viewer. The 123.jpg image under the D Drive is displayed.
Controller-various actionresult [2]