[ASP. NET topic] (6) -- other return values of actionresult in ASP. NET MVC

Source: Internet
Author: User
ArticleDirectory
    • 1. accx page
    • Ii. return text
    • 3. Return JSON
    • 4. Output JS files
    • V. page Jump
    • 6. Display files

In the previous article, all the actions we see are return view (); we can think of this return value to parse An ASPX file. Its return type is actionresult, which is simpleCodeIs:

Public actionresult index () <br/>{< br/> return view (); <br/>}

So what else can we return besides return view? How to return? This is the problem to be solved in this chapter.

1. accx page

Scenario: return code snippets. For example, Ajax returns a subpage,

1. Create an action as follows:

Public actionresult ascx () <br/>{< br/> return partialview (); <br/>}

2. Create another view, right-click the action, and select addview.

Note: select

3. A new ascx page is created, so we can rewrite it as follows:

<Div> <br/> get a div <br/> </div>

The following page is displayed after running:

 

Ii. return text

In addition to the above situations, sometimes we will only return a piece of text.

In this case, we can use the following action form:

Public actionresult text () {<br/> return content ("this is a piece of text"); <br/>}

3. Return JSON

Sometimes, when Ajax is called, the returned object is required to be serialized in JSON format, for example:

Public actionresult showjson () <br/>{< br/> var M = new rocketindexmodel <br/>{< br/> name = "Xue jingming ", <br/> sex = true <br/>}; <br/> return JSON (m); <br/>}

Response text:

{"Name": "Xue jingming", "sex": true}

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 () <br/>{< br/> return JavaScript ("Var x = 0;"); <br/>}

We can access it to get a normal page, but its content-type: Application/X-JavaScript; charset = UTF-8

5. page Jump 5.1 jump to URL
<textarea cols="82" rows="8" name="code" class="c-sharp">Public actionresult rdurl () <br/>{< br/> return redirect ("http://blog.csdn.net/rocket5725"); <br/>}</textarea> 5.2 jump to action

Public actionresult rdaction () <br/>{< br/> return redirecttoaction ("Index", "Rocket"); <br/>}

5.3 jump to the routing rule

Public actionresult rdrouting () <br/>{< br/> return redirecttoroute ("default ", // route name <br/> New {<br/> controller = "Rocket", <br/> action = "Index" <br/> }); <br/>}

6. Display files

Public actionresult FN () <br/>{< br/> return file (<br/> "/content/site.css" // file path <br/>, "text/CSS" // file type <br/>); <br/>}

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.