具体情况是什么? [英] What exactly is a context?

查看:68
本文介绍了具体情况是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC中的上下文一词的确切含义是什么?我知道我们得到了一个databaseContext,一个filterContext和一个httpContext,仅举几例。

它是'object'的同义词吗?这些可以很容易地被命名为databaseObject,filterObject等。



如果我必须编写一个名为'FooContext'的自定义类,后缀会是什么?上下文'告诉我'Foo'?



我尝试过的事情:



谷歌搜索它揭示了以下内容:

上下文是一组数据,通常存储在一个Map或一个自定义类中,作为带有访问器和修饰符的结构。它被使用用于维护状态和在系统内共享信息。



这不告诉我任何事情。绝对存储在类中的任何东西都是数据集合。

解决方案

在MVC中,只要http呼叫命中了正确的路由,就会触发你的控制器动作。上下文是该特定行动的工作环境。正如您所注意到的,它可以包含几个方面。当然,这些都是对象(就像其他一切一样)。不同的情境可能表现得不同;有些在动作运行之间是静态的,有些在每次都不同。

例如,HttpContext将包含有关http调用的信息,包括来自客户端的信息,有关Web服务器的静态和运行时信息,以及由处理管道的元素添加的数据。像会话数据或身份验证信息。管道中的元素将使用(主要是扩展,但也读取)此上下文(运行环境 - 如果您愿意)。实际上,您的行动是此流程中的一步。您可能想要使用环境。例如,您可能想要访问原始请求正文或标题。



您可以将上下文对象作为操作的参数,但这对于你

What exactly is meant by the word 'context' in MVC? I know we get a databaseContext, a filterContext and a httpContext to name a few.
Is it synonymous with 'object'? Could these just as easily have been named databaseObject, filterObject etc.

If I had to write a custom class with the name of 'FooContext', what would the suffix 'context' be telling me about 'Foo'?

What I have tried:

Googling it revealed the following:
"A Context is a collection of data, often stored in a Map or in a custom class which acts as a struct with accessors and modifiers. It is used for maintaining state and for sharing information within a system."

This doesn't tell me anything. Absolutely anything stored in a class is a collection of data.

解决方案

In MVC, your controller action is fired, whenever a proper route is hit by a http call. The context is the working environment of that specific run of the action. It can consist of several things, as you have noticed. Of course, these are objects (as everything else). Different contexts can behave differently; some are sort of static between action runs, some will be different each time.
HttpContext, for example, will contain information about the http call including information from the client, static and runtime information about the web server, but also data that is added by the elements of the processing pipeline. Like session data, or authentication information. Elements in the pipeline will use (mostly extend, but also read) this context (running environment - if you like). Actually, your action is one step in this pipeline. You might want to use the environment, or not. For example, you might want to access the raw request body or header.

You could get the context objects as parameters of the action, but that would be unnecessary headaches for you.


这篇关于具体情况是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆