ASP.NET MVC:什么是操作方法?行动结果?它们有何关系?

发布于 2024-09-09 19:41:09 字数 405 浏览 2 评论 0原文

我很抱歉问这样一个基本的问题,但这对我来说是基本的。为了更好地理解过滤器,我需要理解这个概念。虽然我已经使用 ASP.NET MVC 几个月了,并且正在做一些不错的演示,但我对 Action 方法概念比 Action 结果更熟悉。

什么是:

  1. 行动方法?
  2. 行动结果?
  3. 它们有何关系?

假设我有

public ViewResult ShowPerson(int id)
{
  var friend = db.Persons.Where(p => P.PersonID == id).First(); 
  return View(friend);
}

这些概念如何应用于上面的代码?

谢谢你的帮助。

I'm sorry to ask such a basic question, but it's kind of fundamental for me. To better understand filters, I need to understand this notions. Although I'm on ASP.NET MVC for few months now and now are doing nice demos, I'm more familiar with Action method concept than action result.

What are:

  1. Action Method?
  2. Action Result?
  3. How are they related?

Let's say I've this

public ViewResult ShowPerson(int id)
{
  var friend = db.Persons.Where(p => P.PersonID == id).First(); 
  return View(friend);
}

How those concepts apply to the above code?

Thanks for helping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨与橙与城 2024-09-16 19:41:09

在您的示例中,ShowPerson 是操作。每个操作都需要返回一个操作结果(在您的情况下它返回一个视图)。因此,当调用控制器操作方法时,它会进行一些处理并决定哪个视图最适合表示模型。

您可能会使用许多不同的操作结果。它们都源自 ActionResult

  • ViewResult - 如果您想返回视图
  • FileResult - 如果您想下载文件
  • JsonResult - 如果您想将某些模型序列化为 JSON
  • < a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.contentresult.aspx" rel="noreferrer">ContentResult - 如果您想返回纯文本
  • < a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.redirectresult.aspx" rel="noreferrer">RedirectResult - 如果您想重定向到其他某个操作
  • HttpUnauthorizedResult - 如果您想表明这一点用户无权访问此操作
  • FooBarResult - 您编写的自定义操作结果

In your example ShowPerson is the action. Each action needs to return an action result (In your case it returns a view). So when a controller action method is invoked it does some processing and decides what view would be best adapted to represent the model.

There are many different action results that you might use. They all derive from ActionResult:

  • ViewResult - if you want to return a View
  • FileResult - if you want to download a file
  • JsonResult - if you want to serialize some model into JSON
  • ContentResult - if you want to return plain text
  • RedirectResult - if you want to redirect to some other action
  • HttpUnauthorizedResult - if you want to indicate that the user is not authorized to access this action
  • FooBarResult - a custom action result that you wrote
巴黎夜雨 2024-09-16 19:41:09

@Darin-dimitrov 的回答非常切题。但我也看到 MSDN 上给出的解释非常有帮助。

操作方法通常与用户具有一对一的映射
互动。用户交互的示例包括输入 URL
进入浏览器,单击链接并提交表单。每一个
这些用户交互会导致请求发送到服务器。在
在每种情况下,请求的 URL 都包含 MVC 所指示的信息
框架用于调用操作方法。

当用户在浏览器中输入 URL 时,MVC 应用程序使用
Global.asax 文件中定义的路由规则用于解析
URL 并确定控制器的路径。那么控制器
确定处理请求的适当操作方法。经过
默认情况下,请求的 URL 被视为子路径,其中包括
控制器名称后跟操作名称。例如,如果一个
用户输入 URL http://contoso.com/MyWebSite/Products/Categories
子路径是/P​​roducts/Categories。默认路由规则处理
“Products”作为控制器的前缀名称,必须以
“控制器”(例如 ProductsController)。它将“类别”视为
操作的名称。因此,路由规则调​​用
产品控制器的类别方法,用于处理
要求。如果 URL 以 /Products/Detail/5 结尾,则默认路由
规则将“Detail”视为操作的名称,并且 Detail 方法
调用产品控制器来处理请求。经过
默认情况下,URL 中的值“5”将传递给 Detail 方法
作为参数。

Answer by @Darin-dimitrov is very much upto the point. But I see explanation given on MSDN also very much helpful.

Action methods typically have a one-to-one mapping with user
interactions. Examples of user interactions include entering a URL
into the browser, clicking a link, and submitting a form. Each of
these user interactions causes a request to be sent to the server. In
each case, the URL of the request includes information that the MVC
framework uses to invoke an action method.

When a user enters a URL into the browser, the MVC application uses
routing rules that are defined in the Global.asax file to parse the
URL and to determine the path of the controller. The controller then
determines the appropriate action method to handle the request. By
default, the URL of a request is treated as a sub-path that includes
the controller name followed by the action name. For example, if a
user enters the URL http://contoso.com/MyWebSite/Products/Categories,
the sub-path is /Products/Categories. The default routing rule treats
"Products" as the prefix name of the controller, which must end with
"Controller" (such as ProductsController). It treats "Categories" as
the name of the action. Therefore, the routing rule invokes the
Categories method of the Products controller in order to process the
request. If the URL ends with /Products/Detail/5, the default routing
rule treats "Detail" as the name of the action, and the Detail method
of the Products controller is invoked to process the request. By
default, the value "5" in the URL will be passed to the Detail method
as a parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文