Asp.Net MVC 学习心得 之 Controllers
下面是一个Controller的定义:
public class ProductController : Controller { // // GET: /Product/ public ActionResult Index() { return View(); } public ActionResult Help() { return View(); } public ActionResult Details(int Id) { return View(); } }
一、Action 中 return View()的理解
也就是ActionResult,这里return View()其实就是返回html格式的内容给页面显示,而ActionResult是有很多类型的:
· View() – 返回一个 ViewResult.
· PartialView() – 返回一个 PartialViewResult.
· RedirectToAction() – 返回一个 RedirectToRouteResult .
· Redirect() – 返回一个 RedirectResult.
· Content() – 返回一个 ContentResult.
· Json() – 返回一个 JsonResult.
· File() – 返回一个 FileResult.
· JavaScript() – 返回一个 JavaScriptResult.
· RedirectToRoute() – 返回一个 RedirectToRouteResult.
解释:
· ViewResult – 表示一个普通的 ASP.NET MVC view.
· PartialViewResult – 表示一个ASP.NET MVC view的一个片段.
· RedirectResult – 表示重定向到另外一个controller action 或者 URL.
return RedirectToAction("Index");
return RedirectToAction(“Index”, “Product”);
return RedirectToAction(“Details”, new {id=53});
· ContentResult – 表示发送一些基本的类型的内容给浏览器,只要是.net的基本类型都可以 比如:string,int,double等等
public string SayHello() { return "Hello"; }
· JsonResult – 返回一个Json类型给浏览器
public ActionResult List() { var quotes = new List<string> { "Look before you leap", "The early bird gets the worm", "All hat, no cattle" }; return Json(quotes); }
· FileResult – 返回一个文件,用来下载的
public ActionResult Download() { return File("~/Content/CompanyPlans.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "CompanyPlans.docx"); }
· EmptyResult – 一个action没有返回结果
· HttpUnauthorizedResult – 返回HTTP Unauthorized status code.
· JavaScriptResult – 返回一个JavaScript 文件.
· RedirectToRouteResult – 使用route values重定向到另外一个controller的action
二、如何控制Action被调用
1.使用AcceptVerbs
看代码:
// GET: /Employee/Create [AcceptVerbs(HttpVerbs.Get)] public ActionResult Create() { return View(); } // POST: /Employee/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Employee employeeToCreate) { try { _repository.InsertEmployee(employeeToCreate); return RedirectToAction("Index"); } catch { return View(); } } // DELETE: /Employee/Delete/1 [AcceptVerbs(HttpVerbs.Delete)] public ActionResult Delete(int id) { _repository.DeleteEmployee(id); return Json(true); }
就是控制是Get还是Post的时候触发Create这个Action了
http 支持的动作类型:
· OPTIONS – Returns information about the communication options available.
· GET – Returns whatever information is identified by the request.
· HEAD – Performs the same operation as GET without returning the message body.
· POST – Posts new information or updates existing information.
· PUT – Posts new information or updates existing information.
· DELETE – Deletes information.
· TRACE – Performs a message loop back.
· CONNECT – Used for SSL tunneling.
2.使用ActionName
[ActionName("Edit")] [AcceptVerbs(HttpVerbs.Get)] public ActionResult Edit_GET(Merchandise merchandiseToEdit) { return View(merchandiseToEdit); }
3.使用ActionMethodSelector
namespace MvcApplication1.Selectors { public class AjaxMethod : ActionMethodSelectorAttribute { public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) { return controllerContext.HttpContext.Request.IsAjaxRequest(); } } }
在Controller中的代码:
[AjaxMethod] [ActionName("Index")] public string Index_AJAX() { var selectedIndex = _rnd.Next(_news.Count); return _news[selectedIndex]; }
三、一个特殊处理:请求没有定义的Action的处理
public class CatalogController : Controller { public ActionResult Create() { return View(); } public ActionResult Delete(int id) { return View(); } protected override void HandleUnknownAction(string actionName) { ViewData["actionName"] = actionName; View("Unknown").ExecuteResult(this.ControllerContext); } }
使用HandleUnkonwnAction,默认情况下返回404 Resource Not Found的异常,你可以重载定义
路漫漫其修远兮 吾将上下而求索
my blog