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

推荐订阅源

T
The Blog of Author Tim Ferriss
小众软件
小众软件
S
Schneier on Security
S
Securelist
P
Proofpoint News Feed
D
DataBreaches.Net
博客园_首页
S
Security Affairs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园 - Franky
C
Check Point Blog
GbyAI
GbyAI
H
Help Net Security
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
博客园 - 叶小钗
WordPress大学
WordPress大学
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
C
Cybersecurity and Infrastructure Security Agency CISA
SecWiki News
SecWiki News
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
H
Hacker News: Front Page
宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hacker News - Newest:
Hacker News - Newest: "LLM"
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CERT Recently Published Vulnerability Notes
Google Online Security Blog
Google Online Security Blog
P
Privacy & Cybersecurity Law Blog
N
News | PayPal Newsroom
M
MIT News - Artificial intelligence
P
Privacy International News Feed
博客园 - 三生石上(FineUI控件)
美团技术团队
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
Last Week in AI
Last Week in AI
Spread Privacy
Spread Privacy
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
Engineering at Meta
Engineering at Meta

博客园 - 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);
}