惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
A
About on SuperTechFans
Microsoft Security Blog
Microsoft Security Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
腾讯CDC
Google DeepMind News
Google DeepMind News
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
The Register - Security
The Register - Security
博客园 - Franky
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
B
Blog RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Simon Willison's Weblog
Simon Willison's Weblog
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
S
Schneier on Security
MyScale Blog
MyScale Blog
The Last Watchdog
The Last Watchdog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
博客园 - 聂微东
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Troy Hunt's Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
K
Kaspersky official blog
F
Fortinet All Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
Security Latest
Security Latest
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
B
Blog
有赞技术团队
有赞技术团队
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Latest news
Latest news
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Help Net Security
Help Net Security
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
J
Java Code Geeks

博客园 - Na57

ASP.NET MVC应用中一个诡异错误的处理 使用Enterprise Library 4.0 Logging 的问题 8. Action过滤 6. 视图和生成助手 2. 创建基本的MVC项目 3. URL路由 - Na57 - 博客园 4. 用MVC实现URL路由 Google笔记本迈向烂笔头 XML and Databases 笔记 烂笔头又来了 - Na57 - 博客园 <程序员>200711期算法擂台的解答 GeoServer抛异常IllegalArgumentException的原因 CSS学习 关于CSS中float属性的理解 ItemCreated与ItemDataBound 《SAML简介:安全地共享数字身份信息》读后感 JavaScript 学习(2) - JS的内建对象 JavaScript from C#(入门篇) 2006.6《程序员》笔记
5. 控制器和Action方法
Na57 · 2008-05-06 · via 博客园 - Na57

控制器和Action方法

原文:http://quickstarts.asp.net/3-5-extensions/mvc/MVCControllerActions.aspx

1. 介绍
控制器的职责:定位并执行Action方法,并确保它能正确执行;获取Action方法所需的参数并传递给它;捕获Action方法执行期间出现的错误;提供“WebFormViewFactory”类以便生成ASP.NET页面类型的视图。

一个简单的示例:

namespace MvcApplication.Controllers
{
public class HomeController : Controller
{
public void Index()
{
RenderView("Index");
}

public void About()
{
ViewData["CompanyName"] = "Contoso";
RenderView("About");
}
}
}

2. Action方法
MVC框架默认认为所有public方法都是Action方法。若不想让某个public方法成为Action方法,应该对其使用“NonActionAttribute”属性标记。

3. Action方法的参数
默认地,Action方法的参数是一组键值对,它们来自Form表单、查询字符串和cookie。
控制器基类Controller负责把来自表单数据和RouteData实例的参数传给Action方法。当参数值无法解析时,若参数时引用或Nullable类型,则传递null给它,否则则抛出异常。
为了直接存取URL中的数据,控制器提供了Request和Response类,他们的语义对应于
HttpRequestHttpResponse。下面是一个简单的例子:

public void Detail()
{
int id = Convert.ToInt32(Request["id"]);
}

4. 自动映射Action方法的参数
当HTTP请求中包含与Action方法的参数名字相同的参数时,该参数的值将被自动传递给Action方法。另外,MVC框架同样支持可选参数,当Action方法的参数为nullable类型,且为传递给此参数任何值时,控制器将传递null给此参数。下面是一个示例:

public void ShowArticles(DateTime date)
{
if(!date.HasValue)
{
date = DateTime.Now;
}
// ...
}

5. 处理未知Action
当控制器处理一个未知的Action时,它将抛出HandleUnknownAction异常,控制器默认的处理方式是返回HTTP 404错误。另外,可以通过重写“HandleUnknownError”方法实现自定义的错误处理。下面是一个例子:

public void override HandleUnknownError(string action) {
// Redirect to a search page where the unknown action is
// the search query. Determine when to show the search page
// based on the result of calling a ShouldShowSearch() method.
if (ShouldShowSearch(action) == true)
{
RedirectToAction("search", action );
return;
}
base.HandleUnknownError(action);
}