MVCのActionResult
7738 ワード
一、すべてのControllerはSystemから継承する。Web.Mvc.Controller
現在ASP.NET MVC 3のデフォルトでは、System.Web.Mvcネーミングスペースにあります.
ここで、ActionResultは抽象クラスであり、以下のすべてのResultが継承されるため、1つのActionの戻り値がActionResultであれば、以下のいずれかのタイプの値を返すことができるが、戻り値が以下のいずれかのResultであることを制限すると、指定したタイプのデータのみを返すことができる.
ContentResult EmptyResult FileResult HttpStatusCodeResult HttpNotFoundResult HttpUnauthorizedResult JavaScriptResult JsonResult RedirectResult RedirectToRouteResult ViewResultBase PartialViewResult ViewResult public ContentResult Index()
{
return Content(" "); //
}
public EmptyResult Index()
{
return new EmptyResult(); //
}
public FileResult Index()
{
return File(Server.MapPath("~/demo.jpg"), "application/x-jpg", "demo.jpg"); // demo.jpg
}
public HttpNotFoundResult Index()
{
return HttpNotFound(); // 404
}
public HttpUnauthorizedResult Index()
{
return new HttpUnauthorizedResult(); // , /Account/LogOn
}
public JavaScriptResult hello()
{
string js = "alert(' ?');";
return JavaScript(js); // alert(' ?');} js, js <script src="@Url.Action("hello")" type="text/javascript"></script>
}
public JsonResult Index()
{
var jsonObj = new
{
Id = 1,
Name = " ",
Sex = " ",
Like = " "
};
return Json(jsonObj, JsonRequestBehavior.AllowGet); // JSON, JS
}
public RedirectResult Index()
{
return Redirect("~/demo.jpg"); //
return Redirect("http://www.baidu.com");
return Redirect("/list");
}
public RedirectToRouteResult Index()
{
return RedirectToRoute( // Action
new
{
controller = "Home",
action = "GetName"
});
}
public ViewResult Index()
{
return View(); // ,
//return View("List");
//return View("/User/List");
}
public PartialViewResult Index()
{
return PartialView(); // , , View
}
public ContentResult Index()
{
return Content(" "); //
}
public EmptyResult Index()
{
return new EmptyResult(); //
}
public FileResult Index()
{
return File(Server.MapPath("~/demo.jpg"), "application/x-jpg", "demo.jpg"); // demo.jpg
}
public HttpNotFoundResult Index()
{
return HttpNotFound(); // 404
}
public HttpUnauthorizedResult Index()
{
return new HttpUnauthorizedResult(); // , /Account/LogOn
}
public JavaScriptResult hello()
{
string js = "alert(' ?');";
return JavaScript(js); // alert(' ?');} js, js <script src="@Url.Action("hello")" type="text/javascript"></script>
}
public JsonResult Index()
{
var jsonObj = new
{
Id = 1,
Name = " ",
Sex = " ",
Like = " "
};
return Json(jsonObj, JsonRequestBehavior.AllowGet); // JSON, JS
}
public RedirectResult Index()
{
return Redirect("~/demo.jpg"); //
return Redirect("http://www.baidu.com");
return Redirect("/list");
}
public RedirectToRouteResult Index()
{
return RedirectToRoute( // Action
new
{
controller = "Home",
action = "GetName"
});
}
public ViewResult Index()
{
return View(); // ,
//return View("List");
//return View("/User/List");
}
public PartialViewResult Index()
{
return PartialView(); // , , View
}