Tag Helpers
The EnvironmentTagHelper
can be used to include different scripts in your views (for example, raw or minified) based on the runtime environment, such as Development, Staring, or Production:
<environment names="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
</environment>
<environment names="Staging,Production">
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"
asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
asp-fallback-test="window.jQuery">
</script>
</environment>
formating
By default MVC includes a JsonInputFormatter
class for handling JSON data, but you can add additional formatters for handling XML and other custom formats.可以添加其它的formatting.例如:
services.AddMvc()
.AddXmlSerializerFormatters();或者
services.AddMvc(options =>
{
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
});
Action可以返回一个对象,Controller actions can return POCOs (Plain Old CLR Objects), in which case ASP.NET MVC will automatically create an ObjectResult
for you that wraps the object.
强制格式化返回数据:[Produces]
filter
[Produces("application/json")]
public class AuthorsController
在url中定义数据格式:
[FormatFilter]
public class ProductsController
{
[Route("[controller]/[action]/{id}.{format?}")]
public Product GetById(int id)
Response cache
The primary HTTP header used for caching is Cache-Control
. The HTTP 1.1 specification details many options for this directive. Three common directives are:
- public
- Indicates that the response may be cached.
- private
- 此种情况只能保存个人的缓存中,比如浏览器。
- Indicates the response is intended for a single user and must not be cached by a shared cache. The response could still be cached in a private cache (for instance, by the user’s browser).
- no-cache
- Indicates the response must not be used by a cache to satisfy any subsequent request (without successful revalidation with the origin server).