"Go" ASP. NET MVC Learning Note-controller ActionResult

Source: Internet
Author: User

1. Return to Viewresult
Public ActionResult Index ()
{
viewdata["Message"] = "Welcome to ASP. mvc!";
return View ();
}
Public ActionResult Index ()
{
viewdata["Message"] = "Welcome to ASP. mvc!";
return View ();
}
ActionResult is Viewresult base class
2. Return string
public string GetData (string data)
{
String str = "Return String";
return str;
}
public string GetData (string data)
{
String str = "Return String";
return str;
}
3. Return to Contentresult
Public Contentresult contentstring ()
{
Return content ("This is Content Result Sample");
}
Public Contentresult contentstring ()
{
Return content ("This is Content Result Sample");
}
4. Return to Partialviewresult
Public Partialviewresult partiviewsample ()
{
return Partialview ();
}
Public Partialviewresult partiviewsample ()
{
return Partialview ();
}
5. Return to JavaScript
Public Javascriptresult Jsresult ()
{
string [email protected] "<mce:script language= ' javascript ' ><!--alert (' This is JavaScript result sample);//- ></mce:script> ";
Return JavaScript (JS);
}
Public Javascriptresult Jsresult ()
{
string [email protected] "<mce:script language= ' javascript ' ><!--alert (' This is JavaScript result sample);//- ></mce:script> ";
Return JavaScript (JS);
}
6. Return the JSON string
Public Jsonresult jsonstring ()
{
Return Json (new{name= "name", sex= "Sex"});
}
Public Jsonresult jsonstring ()
{
Return Json (new{name= "name", sex= "Sex"});
}
7. Return to Fileresult
Public Fileresult filesample ()
{
Return File (
"/content/site. CSS ",//file path
"Text/css"//File type

);

//-----------------------------------------------

The controller returns a value type ActionResult in ASP.
All controller classes in MVC must be named with the "controller" suffix
And there are certain requirements for action:
? must be a public method
? must be an instance method
No sign of Nonactionattribute characteristics (noaction)
? cannot be overloaded
? The ActionResult type must be returned
Such as:
public class Mycontroller:controller
{
Must return ActionResult type
Public ActionResult HelloWorld ()
{
viewdata["Message"] = "Hello world!";
return View ();
}
}
public class Mycontroller:controller
{
Must return ActionResult type
Public ActionResult HelloWorld ()
{
viewdata["Message"] = "Hello world!";
return View ();
}
}
The following lists the ActionResult return types in the controller in ASP.
1. Return the Viewresult view result to render the view to the Web page
Public ActionResult About ()
{
return View (); Parameter can return a model object
}
Public ActionResult About ()
{
return View (); Parameter can return a model object
}
2, return partialviewresult partial view result, mainly used to return partial view content
Create a viewusercontrol.cshtml partial view under the View/shared directory
Public ActionResult UserControl ()
{
Viewbag.message = "partial view";
Return Partialview ("Viewusercontrol");
}
Public ActionResult UserControl ()
{
Viewbag.message = "partial view";
Return Partialview ("Viewusercontrol");
}
Page call @viewbag.message will output "partial view"
3. Return contentresult user-defined content type
Public ActionResult Content ()
{
Return content ("Test content", "text/html"); You can specify a text type
}
Public ActionResult Content ()
{
Return content ("Test content", "text/html"); You can specify a text type
}
Page output "Test Content";
This type is used for text content that needs to be returned in an AJAX operation
4. Return Jsonresult serialized JSON object
Public ActionResult Json ()
{
dictionary<string, object> dic = new dictionary<string, object> ();
Dic. ADD ("id", 100);
Dic. ADD ("name", "Hello");
Return Json (DIC, Jsonrequestbehavior.allowget);
}
Public ActionResult Json ()
{
dictionary<string, object> dic = new dictionary<string, object> ();
Dic. ADD ("id", 100);
Dic. ADD ("name", "Hello");
Return Json (DIC, Jsonrequestbehavior.allowget);
Used primarily to return JSON-formatted objects, which can be manipulated using Ajax;
Note: Parameters need to be set, Jsonrequestbehavior.allowget,
Otherwise, an error will be prompted: This request has been blocked because sensitive information is disclosed to third-party Web sites when used in a GET request.
To allow a GET request, set Jsonrequestbehavior to Allowget.
5. Return Javascriptresult scripts that can be executed at the client
Public ActionResult JavaScript ()
{
String str = string. Format ("Alter (' {0} ');", "Pop-up Window");
return JavaScript (str);
}
Public ActionResult JavaScript ()
{
String str = string. Format ("Alter (' {0} ');", "Pop-up Window");
return JavaScript (str);
However, this does not directly respond to pop-ups and requires a page to be called again.
This makes it easy to perform different JS operations based on different logic.
6. Return Fileresult to write the binary output in the response, which can generally be used as a function for simple download
Public ActionResult File ()
{
String fileName = "~/content/test.zip"; Filename
String downfilename = "file display name. zip"; The file name to display in the download box
Return File (FileName, "Application/octet-stream", downfilename);
}
Public ActionResult File ()
{
String fileName = "~/content/test.zip"; Filename
String downfilename = "file display name. zip"; The file name to display in the download box
Return File (FileName, "Application/octet-stream", downfilename);
After you download test.zip directly, save to local file display name. zip
7. Return null or void data type Emptyresult
Public ActionResult Empty ()
{
return null;
}
Public ActionResult Empty ()
{
return null;
} returns Null
8. Redirect method: Redirect/redirecttoaction/redirecttoroute
Redirect: Go directly to the specified URL address
Public ActionResult Redirect ()
{
Returns the specified URL address directly
Return Redirect ("http://www.baidu.com");
}
Public ActionResult Redirect ()
{
Returns the specified URL address directly
Return Redirect ("http://www.baidu.com");
} redirecttoaction: Use the Action Name to jump directly, or you can add controllername
Public ActionResult Redirectresult ()
{
Return redirecttoaction ("Index", "Home", new {id = "+", name = "Liu"});
}
Public ActionResult Redirectresult ()
{
Return redirecttoaction ("Index", "Home", new {id = "+", name = "Liu"});
} You can also take the parameters
Redirecttoroute: Specify a route to jump
Public ActionResult Redirectrouteresult ()
{
Return Redirecttoroute ("Default", new {controller = "Home", action = "Index"});
}
Public ActionResult Redirectrouteresult ()
{
Return Redirecttoroute ("Default", new {controller = "Home", action = "Index"});
}default the route name as defined in Global.asax.cs

"Go" ASP. NET MVC Learning Note-controller ActionResult

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.