Introduction

This document describes ASP.NET Core integration for ASP.NET Boilerplate framework. ASP.NET Core integration is implemented in Abp.AspNetCore nuget package

本文档介绍了ASP.NET样板ASP.NET核心集成框架。ASP.NET的核心集成在abp.aspnetcore NuGet包实现

Migrating to ASP.NET Core?(迁移到ASP.NET Core)

If you have an existing project and considering to migrate to ASP.NET Core, you can read our blog post for our experince on the migration.

如果你有一个现有的项目,考虑到ASP.NET的核心,你可以阅读我们的博客,我们在迁移中的经验。

Startup Template(启动模板)

You can create your project from startup template, which is a simple, empty web project but properly integrated and configured to work with ABP framework.

您可以从启动模板创建项目,这是一个简单的、空的Web项目,但经过适当的集成和配置,可以与ABP框架一起工作。

Configuration

Startup Class

To integrate ABP to ASP.NET Core, we should make some changes in the Startup class as shown below:

public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//... //Configure Abp and Dependency Injection. Should be called last.
return services.AddAbp<MyProjectWebModule>(options =>
{
//Configure Log4Net logging (optional)
options.IocManager.IocContainer.AddFacility<LoggingFacility>(
f => f.UseLog4Net().WithConfig("log4net.config")
);
});
} public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//Initializes ABP framework and all modules. Should be called first.
app.UseAbp(); //...
}
}

Module Configuration(模块配置)

You can use startup configuration to configure AspNet Core Module (using Configuration.Modules.AbpAspNetCore() in PreInitialize of your module).

你可以使用启动配置配置ASPNET核心模块(使用配置模块。abpaspnetcore()在分发你的模块)。

Controllers

Controllers can be any type of classes in ASP.NET Core. It's not restricted to classes derived from Controller class. By default, a class ends with Controller (like ProductController) is considered as MVC Controller. You can also add MVC's [Controller] attribute to any class to make it a controller. This is the way ASP.NET Core MVC is working. See ASP.NET Core documentation for more.

If you will use web layer classes (like HttpContext) or return a view, it's better to inherit from AbpController (which is derived from MVC's Controller) class. But if you are creating an API controller just works with objects, you can consider to create a POCO controller class or you can use your application services as controllers as described below.

控制器可以在ASP.NET核心的任何类型的类。它不限于从控制器类派生的类。默认情况下,一类以控制器(如ProductController)作为MVC控制器。您还可以向任何类添加MVC的[控制器]属性,使之成为控制器。这是ASP.NET核心的MVC工作。更多的看到ASP.NET的核心文件。

如果你会使用Web层类(如HttpContext)或返回一个视图,这是更好地继承abpcontroller(这是从MVC的控制器)类。但是如果你正在创建一个API控制器只是工作的对象,你可以考虑创建一个POCO控制器类或您可以使用您的应用程序服务作为控制器,如下所述。

Application Services as Controllers

ASP.NET Boilerplate provides infrastructure to create application services. If you want to expose your application services to remote clients as controllers (as previously done using dynamic web api), you can easily do it by a simple configuration in PreInitialize method of your module. Example:

ASP.NET样板提供基础设施创建应用程序服务。如果你想让你的应用服务远程客户作为控制器(如先前使用动态Web API),你可以很容易做到的一个简单的配置分发你的模块的方法。例子:

Configuration.Modules.AbpAspNetCore().CreateControllersForAppServices(typeof(MyApplicationModule).Assembly, moduleName: 'app', useConventionalHttpVerbs: true);

CreateControllersForAppServices method gets an assembly and converts all application services to MVC controllers in that assembly. You can use RemoteService attribute to enable/disable it for method or class level.

createcontrollersforappservices方法获取一个组件,将所有的应用服务在MVC控制器组件。你可以使用RemoteService属性来启用/禁用它的类或方法的水平。

When an application service is converted to MVC Controller, it's default route will be like /api/services/<module-name>/<service-name>/<method-name>. Example: If ProductAppService defines a Create method, it's URL will be/api/services/app/product/create (assumed that module name is 'app').

当应用程序服务被转换为MVC控制器时,它的默认路由将类似于/api/services/<module-name>/<service-name>/<method-name>。例如:如果productappservice定义一个创建方法,它的URL将/api/services/app/product/create/创建(假设模块的名称是“app”)。

If useConventionalHttpVerbs set to true (which is the default value), then HTTP verbs for service methods are determined by naming conventions:

如果useconventionalhttpverbs设置为true(这是默认值),然后通过命名约定确定HTTP动词使用方法:

  • Get: Used if method name starts with 'Get'.
  • Put: Used if method name starts with 'Put' or 'Update'.
  • Delete: Used if method name starts with 'Delete' or 'Remove'.
  • Post: Used if method name starts with 'Post', 'Create' or 'Insert'.
  • Patch: Used if method name starts with 'Patch'.
  • Otherwise, Post is used as default HTTP verb.

You can use any ASP.NET Core attributes to change HTTP methods or routes of the actions (but surely, this requires to add reference to related ASP.NET Core package).

Note: Previously, dynamic web api system was requiring to create service interfaces for application services. But this is not required for ASP.NET Core integration. Also, MVC attributes should be added to the service classes, even you have interfaces.

你可以使用任何ASP.NET的核心属性改变HTTP方法或路由的行为(当然,这需要添加参考相关ASP.NET核心包)。

注意:以前,动态Web API系统要求为应用程序服务创建服务接口。但这并不是ASP.NET的核心集成所需。另外,MVC属性应该添加到服务类中,即使您有接口。

Filters

ABP defines some pre-built filters for AspNet Core. All of them are added to all actions of all controllers by default.

ABP定义了预建的过滤器ASPNET核心。所有这些都默认添加到所有控制器的所有操作中。

Authorization Filter(授权过滤)

AbpAuthorizationFilter is used to integrate to authorization system and feature system.

abpauthorizationfilter是用来整合授权系统和特征系统。

  • You can define AbpMvcAuthorize attribute for actions or controllers to check desired permissions before action execution.
  • You can define RequiresFeature attribute for actions or controllers to check desired features before action execution.
  • You can define AllowAnonymous (or AbpAllowAnonymous in application layer) attribute for actions or controllers to suppress authentication/authorization.
  • 你可以定义为行动或控制器在执行检查所需的权限abpmvcauthorize属性。
    你可以定义为行动或控制器在执行检查所需的功能requiresfeature属性。
    你可以定义allowanonymous(或abpallowanonymous在应用层)的行动或控制器来抑制认证/授权属性。

Audit Action Filter(审计行为过滤)

AbpAuditActionFilter is used to integrate to audit logging system. It logs all requests to all actions by default (if auditing is not disabled). You can control audit logging using Audited and DisableAuditing attributes for actions and controllers.

abpauditactionfilter是用来整合审计日志系统。默认情况下,它将所有请求记录到所有操作(如果没有禁用审核)。你可以控制审计日志审计和disableauditing使用动作和控制器属性。

Validation Action Filter(动作验证过滤器)

AbpValidationActionFilter is used to integrate to validation system and automatically validate all inputs of all actions. In addition to ABP's built-in validation & normalization, it also checks MVC's Model.IsValid property and throws validation exception if action inputs have any invalid value.

You can control validation using EnableValidation and DisableValidation attributes for actions and controllers.

abpvalidationactionfilter用于集成验证系统和自动验证所有输入的所有行动。此外,ABP的内置验证和标准化,它还检查model.isvalid财产将MVC的验证异常如果有任何动作输入的值无效。

你可以控制使用动作和控制器enablevalidation和disablevalidation属性验证。

Unit of Work Action Filter(工作单元动作过滤)

AbpUowActionFilter is used to integrate to Unit of Work system. It automatically begins a new unit of work before an action execution and completes unit of work after action exucition (if no exception is thrown).

You can use UnitOfWork attribute to control behaviour of UOW for an action. You can also use startup configuration to change default unit of work attribute for all actions.

abpuowactionfilter是用来整合的工作系统。它会自动开始一个新的工作单位前一个动作的执行和完成行动后exucition工作单位(如果没有抛出异常)。

你可以使用属性来控制一个动作UnitOfWork UOW的行为。
还可以使用启动配置更改所有操作的默认工作单元属性。

Exception Filter(异常过滤)

AbpExceptionFilter is used to handle exceptions thrown from controller actions. It handles and logs exceptions and returns wrapped response to the client.

  • It only handles object results, not view results. So, actions returns any object, JsonResult or ObjectResult will be handled. Action returns a view or any other result type implements IActionsResult are not handled. It's suggested to use built-in UseExceptionHandler extension method defined in Microsoft.AspNetCore.Diagonistics package to handle view exceptions.
  • Exception handling and logging behaviour can be changed using WrapResult and DontWrapResult attributes for methods and classes.
  • abpexceptionfilter是用来处理从控制器动作抛出的异常。它处理和记录异常并返回对客户机的包装响应。

    它只处理对象结果,而不查看结果。所以,行动返回任何对象,objectresult JsonResult或将处理。动作返回一个视图或任何其他结果类型实现了iactionsresult处理不。建议使用内置的useexceptionhandler扩展方法在microsoft.aspnetcore.diagonistics包定义视图的例外处理。
    异常处理和日志记录行为可以使用方法和类wrapresult和dontwrapresult属性改变。

Result Filter(结果过滤)

AbpResultFilter is mainly used to wrap result action if action is successfully executed.

  • It only wraps results for JsonResult, ObjectResult and any object which does not implement IActionResult (and also their async versions). If your action is returning a view or any other type of result, it's not wrapped.
  • WrapResult and DontWrapResult attributes can be used for methods and classes to enable/disable wrapping.
  • You can use startup configuration to change default behaviour for result wrapping.
  • abpresultfilter主要用于包装的行动,如果行动是成功执行的结果。

    这只包的结果objectresult JsonResult,任何对象没有实现iactionresult(也是他们的异步版本)。如果您的操作返回视图或其他类型的结果,则不会包装。
    wrapresult和dontwrapresult属性可用于启用/禁用的包装方法和类。
    可以使用启动配置更改结果包装的默认行为。

Result Caching For Ajax Requests(用于Ajax请求的结果缓存)

AbpResultFilter adds Cache-Control header (no-cache, no-store...) to the response for AJAX Requests. Thus, it prevents browser caching of AJAX responses even for GET requests. This behaviour can be disabled by the configuration or attributes. You can use NoClientCache attrbiute to prevent caching (default) or AllowClientCache attrbiute to allow browser to cache results. Alternatively you can implement IClientCacheAttribute to create your special attribute for finer control.

abpresultfilter添加缓存控制头(没有缓存,没有存储…)的响应Ajax请求。因此,即使GET请求,它也阻止浏览器缓存Ajax响应。可以通过配置或属性禁用此行为。你可以使用noclientcache attrbiute防止缓存(默认)或allowclientcache attrbiute让浏览器缓存结果。或者你可以实现iclientcacheattribute为更好的控制创建您的特殊属性。

Model Binders(模型绑定)

AbpDateTimeModelBinder is used to normalize DateTime (and Nullable<DateTime>) inputs using Clock.Normalize method.

abpdatetimemodelbinder用来规范datetime(and Nullable<DateTime>)投入使用的时钟规范方法。

Views

MVC Views can be inherited from AbpRazorPage to automatically inject most used infrastructure (LocalizationManager, PermissionChecker, SettingManager... etc.). It also has shortcut methods (like L(...) for localize texts). Startup template inherits it by default.

You can inherit your web components from AbpViewComponent instead of ViewComponent to take advantage of base properties and methods.

MVC视图可以继承从AbpRazorPage到自动注入最常用的基础(localizationmanager,PermissionChecker,settingmanager…等)。它也有快捷方法(如本地化文本的L(…))。启动模板默认继承它。

你可以继承您的Web部件代替ViewComponent abpviewcomponent利用基地的属性和方法。

Client Proxies(客户端代理)

ABP can automatically create javascript proxies for all MVC Controllers (not only application services). It's created for Application Services as Controllers (see the section above) by default. You can add [RemoteService] attribute to any MVC controller to create client proxy for it. Javascript proxies are dynamically generated on runtime. You need to add given script definition to your page:

ABP可以为所有MVC控制器(不仅仅是应用程序服务)自动创建JavaScript代理。它是为应用程序服务创建的,默认情况下是控制器(参见以上部分)。您可以添加【RemoteService ]属性的任何MVC控制器为它创建客户端代理。JavaScript代理是在运行时动态生成的。您需要将给定的脚本定义添加到页面中:

<script src="~/AbpServiceProxies/GetAll?type=jquery" type="text/javascript"></script>

Currently, only JQuery proxies are generated. We can then call an MVC method with javascript as shown below:

目前只生成jQuery代理。然后,我们可以调用JavaScript的MVC方法,如下所示:

abp.services.app.product.create({
name: 'My test product',
price: 99
}).done(function(result){
//...
});

Integration Testing(集成测试)

Integration testing is fairly easy for ASP.NET Core and it's documented it's own web site in details. ABP follows this guide and provides AbpAspNetCoreIntegratedTestBase class in Abp.AspNetCore.TestBase package. It makes integration testing even easier.

It's better to start by investigating integration tests in startup template to see it in action.

集成测试是相当容易的,这是ASP.NET核心的细节记录在自己的网站。公司遵循本指南和abp.aspnetcore.testbase包提供了abpaspnetcoreintegratedtestbase类。它使集成测试更加容易。

最好先调查启动模板中的集成测试,看看它是否生效。

ABP框架系列之十一:(AspNet-Core-ASPNET核心)的更多相关文章

  1. ABP框架系列之四十一:(Nuget-Packages-Nuget包)

    Packages ASP.NET Boilerplate is distributed on nuget. Here, a list of all official packages. Abp Cor ...

  2. ABP框架系列之五十一:(Timing-定时)

    Introduction While some applications target a single timezone, some others target to many different ...

  3. ABP框架系列之三十一:(Localization-本地化)

    Introduction Any application has at least one language for user interface. Many applications have mo ...

  4. 2019 年起如何开始学习 ABP 框架系列文章-开篇有益

    2019 年起如何开始学习 ABP 框架系列文章-开篇有益 [[TOC]] 本系列文章推荐阅读地址为:52ABP 开发文档 https://www.52abp.com/Wiki/52abp/lates ...

  5. 老周的ABP框架系列教程 -》 一、框架理论初步学习

    老周的ABP框架系列教程 -- 一.框架理论初步学习   1. ABP框架的来源与作用简介 1.1  简介 1.1.1       ABP框架全称为"ASP.NET Boilerplate ...

  6. ABP框架系列之四十七:(SignalR-Integration-SignalR-集成)

    Introduction Abp.Web.SignalR nuget package makes it easily to use SignalR in ASP.NET Boilerplate bas ...

  7. ABP框架系列之五十三:(Web-API-Controllers-Web-API-控制器)

    Introduction ASP.NET Boilerplate is integrated to ASP.NET Web API Controllers via Abp.Web.Api nuget ...

  8. ABP框架系列之五十四:(XSRF-CSRF-Protection-跨站请求伪造保护)

    Introduction "Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a maliciou ...

  9. ABP框架系列之三十四:(Multi-Tenancy-多租户)

    What Is Multi Tenancy? "Software Multitenancy refers to a software architecture in which a sing ...

随机推荐

  1. 虚拟机克隆之后,网卡名称从eth0变成eth1之后的解决办法

    使用VMware安装了CentOS虚拟机,克隆之后使用service network restart指令来重新启动网络服务时,会看到有eth0网卡不存在的提示.出现这种现象的原因是,很多Linux d ...

  2. CIF 搜索逻辑

    test code #include <cstddef> class CIF { }; template <typename OBJ> class CList { public ...

  3. ubuntu18.04 安装mysql server

    mysql 5.7支持的最高版本是Ubuntu17 ,即使安装成功后,也会出现各种妖蛾子,本人就被这种问题困扰了好一会.在Ubuntu 18.04下安装mysql,建议安装8.0以上版本! 1. 配置 ...

  4. java 中文繁简体转换工具 opencc4j

    创作缘由 对于中文的繁简体转换是一种很常见的需求. 但是很多工具类都是简单的做个映射.(使用map,集合,properties)等. 存在一个严重的问题:特殊词组 的转换可能存在问题. OpenCC ...

  5. SQL Server事务

    事务全部是关于原子性的.原子性的概念是指可以把一些事情当做一个单元来看待.从数据库的角度看,它是指应全部执行或全部都不执行的一条或多条语句的最小组合.为了理解事务的概念,需要能够定义非常明确的边界.事 ...

  6. 【函数】raise 函数(小窗help)

    在Python中,要想引发异常,最简单的形式就是输入关键字raise,后跟要引发的异常的名称. 异常名称标识出具体的类: Python异常处理是那些类的对象. 执行raise语句时,Python会创建 ...

  7. Android开发中常见的设计模式(四)——策略模式

    策略模式定义了一些列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变换. 假设我们要出去旅游,而去旅游出行的方式有很多,有步行,有坐火车,有坐飞机等等 ...

  8. Dubbo+zookeeper面试题补充

    什么是分布式?什么是集群?主要区别 分布式是将一个服务分个部分,然后通过远程调用方式进行.远程调用框架RPC框架,spring cloud,dubbo.集群是将同一个服务的多个副本部署在不同的集群上, ...

  9. faster-RCNN框架之rpn 较小目标检测,如果只使用rpn,并减少多个候选框

    通常faster-rcnn目标检测有两个步骤,一个是侯选框生成,一个是侯选框微调+目标区分,但是对于单目标识别, 我经常喜欢只使用rpn网络,效果还不错,不过仅仅的rpn使用参考的参数通常会造成一个目 ...

  10. idea常用快捷键及操作

    ctrl+j  ===== 智能提示 可用模版及关键字 ctrl+p ===== 显示方法可填入的参数 ctrl+space ===== 补全提示项目中可用的变量 ctrl+shift+j  ==== ...