MVC Controller return format

Source: Internet
Author: User

The action you see is a return View (); we can treat this as a return value for parsing an ASPX file. And its return type is actionresult as

Public ActionResult Index ()
{
return View ();
}
Besides view () what value can we use here to return?

I. ASCX page

Scenario: To return a snippet, such as Ajax, to return a sub-page

Let's start with a new action

Public ActionResult Ascx ()
{
return Partialview ();
}
We'll build a view below, still in action, right-click, AddView.

Note the picture is checked.

So I created an ascx page, and we're going to rewrite it a little bit.

<%@ Control language= "C #" inherits= "System.Web.Mvc.ViewUserControl"%> <div> get a div </div>
Run, get the page


Second, return text

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

At this point we can use the following action form:

Public ActionResult Text ()
{
Return Content ("This is a piece of text");
}
Third, return JSON

Sometimes when we call Ajax we also ask that the returned object be the result of JSON serialization, such as:

Public ActionResult Showjson ()
{
var m = new Eiceindexmodel{name = "Kingjian", Sex = true};
Return Json (m);
}
Return text:

{"Name": "Kingjian", "Sex": true}
Iv. Output JS file

Most of the time JS files are static, but sometimes JS files may also be generated dynamically at this point we can output

Public ActionResult Js ()
{
return JavaScript ("var x=0;");
}
We visit, get a normal page but its content-type:application/x-javascript; Charset=utf-8

Five, page jump

1. Jump to URL

Public ActionResult Rdurl ()
{
Return Redirect ("http://www.baidu.com");
}
2. Jump to Action

Public ActionResult rdaction ()
{
Return redirecttoaction ("Index", "Eice");
}
3. Jump to Routing Rule

Public ActionResult rdrouting ()
{
Return Redirecttoroute ("Default",//route name
New{controller = "Eice", Action = "Index"});
}
Vi. Display Files

Public actionresult FN ()
{
Return file ("/content/site.css"//File path
, "TEXT/CSS"//File type
);
}

MVC Controller return format

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.