Asp.Net MVC 学习心得 之 Controllers

简介:
  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);  
        }  
  
  
    }

本文转自 lu xu 博客园博客,原文链接:  http://www.cnblogs.com/dotLive/archive/2009/03/09/1406997.html ,如需转载请自行联系原作者

相关文章
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
41 0
|
1月前
|
开发框架 前端开发 .NET
进入ASP .net mvc的世界
进入ASP .net mvc的世界
29 0
|
1月前
mvc.net分页查询案例——mvc-paper.css
mvc.net分页查询案例——mvc-paper.css
5 0
|
1月前
|
开发框架 前端开发 .NET
C# .NET面试系列六:ASP.NET MVC
<h2>ASP.NET MVC #### 1. MVC 中的 TempData\ViewBag\ViewData 区别? 在ASP.NET MVC中,TempData、ViewBag 和 ViewData 都是用于在控制器和视图之间传递数据的机制,但它们有一些区别。 <b>TempData:</b> 1、生命周期 ```c# TempData 的生命周期是短暂的,数据只在当前请求和下一次请求之间有效。一旦数据被读取,它就会被标记为已读,下一次请求时就会被清除。 ``` 2、用途 ```c# 主要用于在两个动作之间传递数据,例如在一个动作中设置 TempData,然后在重定向到另
95 5
|
3月前
|
XML 前端开发 定位技术
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
C#(NET Core3.1 MVC)生成站点地图(sitemap.xml)
25 0
|
3月前
|
前端开发
.net core mvc获取IP地址和IP所在地(其实是百度的)
.net core mvc获取IP地址和IP所在地(其实是百度的)
123 0
|
5月前
|
开发框架 自然语言处理 前端开发
基于ASP.NET MVC开发的、开源的个人博客系统
基于ASP.NET MVC开发的、开源的个人博客系统
52 0
|
8月前
|
SQL 开发框架 前端开发
[回馈]ASP.NET Core MVC开发实战之商城系统(完:内附源码)
经过一段时间的准备,【ASP.NET Core MVC开发实战之商城系统】已经完成,目前代码已开发完成,先将全部内容整理分享,如有不足之处,还请指正。
107 0