ASP. net mvc key tutorial

Source: Internet
Author: User

The actions we see above are all return view (); we can think of this return value to parse An ASPX file. And its return type is actionresult, such

 
PublicActionresult index (){ReturnView ();}

In addition to view (), what values can we return here?

I. ascx page

Scenario: ReturnCodeFor example, Ajax returns a subpage.

Create an action first.

 
PublicActionresult ascx (){ReturnPartialview ();}

Next we will create another view, which is still right-clicked in the action and addview.

Note that this option is selected in the figure.

So we created an ascx page, so we can rewrite it less.

<% @ Control Language = "C #" inherits = "system. Web. MVC. viewusercontrol" %><Div>Get a div</Div>

Run to get the page

 

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:

 
PublicActionresult text (){ReturnContent ("This is a piece of text.");}
3. Return JSON

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

PublicActionresult showjson () {var M =NewEiceindexmodel {name ="ZOU Jian", Sex =True};ReturnJSON (m );}

Response text:

 
{"Name":"ZOU Jian","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

 
PublicActionresult JS (){ReturnJavaScript ("VaR x = 0;");}

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

V. page Jump

1. Jump to URL

 
PublicActionresult rdurl (){ReturnRedirect ("Http://www.baidu.com");}

2. Jump to action

 
PublicActionresult rdaction (){ReturnRedirecttoaction ("Index","Eice");}

3. Jump to the routing rule

PublicActionresult rdrouting (){ReturnRedirecttoroute ("Default",// Route nameNew{Controller ="Eice", Action ="Index"});}
6. Display files
 
PublicActionresult FN (){ReturnFile ("/Content/site.css"// File path ,"Text/CSS"// File type );}

Let's talk about filter in the next section.

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.