System.Web.Mvc命名空间 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();
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步