(转)ASP.NET MVC3默认提供了11种ActionResult的实现
ActionResult
ContentResult
EmptyResult
FileResult
HttpStatusCodeResult
HttpNotFoundResult
HttpUnauthorizedResult
JavaScriptResult
JsonResult
RedirectResult
RedirectToRouteResult
ViewResultBase
PartialViewResult
ViewResult
?
示例代碼:
public class ActionResultController : Controller
??? {
??????? public ActionResult Index()
??????? {
??????????? return View();
??????? }
?
??????? public ActionResult ContentResult()
??????? {
??????????? return Content("Hi, 我是ContentResult結果");
??????? }
?
??????? public ActionResult EmptyResult()
??????? {
??????????? //空結果當然是空白了!
??????????? //至于你信不信, 我反正信了
??????????? return new EmptyResult();
??????? }
?
??????? public ActionResult FileResult()
??????? {
??????????? var imgPath = Server.MapPath("~/demo.jpg");
?
??????????? return File(imgPath, "application/x-jpg", "demo.jpg");
??????? }
?
??????? public ActionResult HttpNotFoundResult()
??????? {
??????????? return HttpNotFound("Page Not Found");
??????? }
?
??????? public ActionResult HttpUnauthorizedResult()
??????? {
??????????? //未驗證時,跳轉到Logon
??????????? return new HttpUnauthorizedResult();
??????? }
?
??????? public ActionResult JavaScriptResult()
??????? {
??????????? string js = "alert(\"Hi, I'm JavaScript.\");";
?
??????????? return JavaScript(js);
??????? }
?
??????? public ActionResult JsonResult()
??????? {
??????????? var jsonObj = new
??????????? {
??????????????? Id = 1,
??????????????? Name = "小銘",
??????????????? Sex = "男",
??????????????? Like = "足球"
??????????? };
?
??????????? return Json(jsonObj, JsonRequestBehavior.AllowGet);
??????? }
?
??????? public ActionResult RedirectResult()
??????? {
??????????? return Redirect("~/demo.jpg");
??????? }
?
??????? public ActionResult RedirectToRouteResult()
??????? {
??????????? return RedirectToRoute(new {
??????????????? controller = "Hello", action = ""
??????????? });
??????? }
?
??????? public ActionResult ViewResult()
??????? {
??????????? return View();
??????? }
?
??????? public ActionResult PartialViewResult()
??????? {
??????????? return PartialView();
??????? }
?
??????? //禁止直接訪問的ChildAction
??????? [ChildActionOnly]
??????? public ActionResult ChildAction()
??????? {
??????????? return PartialView();
??????? }
?
??????? //正確使用ChildAction
??????? public ActionResult UsingChildAction()
??????? {
??????????? return View();
??????? }
??? }
轉載于:https://www.cnblogs.com/q149072205/archive/2012/03/21/2409810.html
總結
以上是生活随笔為你收集整理的(转)ASP.NET MVC3默认提供了11种ActionResult的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#中showDialog()与show
- 下一篇: 关于IC卡的编程