ASP. NET Core
- Introduced
- Launch template
- Configuration
- Start class
- Module configuration
- Controller
- Application services as a controller
- Filter filters
- Authorization Filter
- Audit Action Filter
- Check Filter
- Work Unit Action Filter
- Exception filter
- Result Filter
- Result caching for AJAX requests
- Model Binder
- View
- Client Agent
- Integration Testing
Introduced
This document describes how the ABP integrates with ASP. ASP. NET core is integrated through the abp.aspnetcore nuget package.
migrating to ASP.
If you already have a project and are considering migrating to ASP. NET Core, you can read our blog post to learn about our migration experience.
Launch template
You can create a project in the startup template, which is a simple, empty Web project but is already integrated with the ABP framework and configured appropriately.
Configuration
Start class
In order for the ABP to be integrated into ASP. NET Core, we need to make some changes in the startup class as follows:
Public classstartup{ Public IServiceProvider configureservices (iservicecollection services) {//... //Configure Abp and Dependency injection. Should was called last. return services. Addabp<myprojectwebmodule> (options + = //Configure log4net Logging (opti onal) options. Iocmanager.ioccontainer.addfacility<loggingfacility>(f = F.uselog4net (). Withconfig ("log4net.config" ) ); }); } Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { //initializes ABP framework and all modules. should be called first. app. USEABP (); //... }}
Module configuration
You can use the boot configuration To configure the ASPNET core module (using Configuration.Modules.AbpAspNetCore () in the Preinitialize method of the module).
Controller
in ASP. NET core, the controller can be any type of class. It is not limited to classes that inherit from controller. A class defaults to the MVC controller at the end of a controller (such as Productcontroller). You can also add MVC's [controller] attribute to any class to make it a controller. This is how ASP. NET Core MVC works. See the ASP. NET Core documentation for more information.
If you use a Web layer class (such as HttpContext) or return a view, it is best to inherit the Abpcontroller(which inherits from the Controller of MVC) class. However, if you create an API controller that uses only objects, consider creating a Poco controller class or you can use app service as the controller as described below.
Application services as a controller
ABP provides the infrastructure to create app services . If you want to provide application services as a controller to remote clients (you can use the Dynamic Web APIas before), you can do this in a simple configuration in the Preinitialize method of the module. Example:
Configuration.Modules.AbpAspNetCore (). createcontrollersforappservices (typeof'app'true);
createcontrollersforappservices method receives an assembly that converts all application services in the assembly to an MVC controller. You can use the Remoteservice attribute to enable/disable it at the method or class level.
When an app service is converted to an MVC controller, the default route is/api/services/<module-name>/<service-name>/<method-name>. For example: If Productappservice defines a create method, its URL will be /api/services/app/product/create(assuming the module's name is ' app ').
If useconventionalhttpverbs is set to True (the default value is true), then the HTTP verb for the service method is determined by the naming convention :
- Get : Method name to ' Get ' is used at the beginning.
- Put : The method name is used at the start of ' Put ' or ' Update '.
- Delete : The method name is used at the beginning of ' Delete ' or ' Remove '.
- Post : The method name is used at the beginning of ' Post ', ' Create ' or ' Insert '.
- Patch : The method name is used at the beginning of ' Patch '.
- otherwise, Post is the HTTP verb used by default .
You can use any of the ASP. NET core features to change the routing of HTTP methods or actions (but of course this requires adding a reference to the relevant ASP. NET Core package).
Note : You will need to dynamically Wei the API system before creating a service interface to the app service, but not for the ASP. Similarly, the MVC attribute should be added to the service class, even if there is already an interface.
Filter filters
The ABP defines some built-in filters for the ASPNET Core. The default all filters are added to all actions of all controllers.
Authorization Filter
Abpauthorizationfilter used to integrate authorization systems and feature systems.
- You can define the abpmvcauthorize attribute for actions or controllers to check the permissions of a requirement before execution.
- You can define requiresfeature attributes for actions or controllers to examine the characteristics of requirements before execution.
- You can define allowanonymous(or abpallowanoymous-defined) attributes for actions or controllers to suppress checksum authorization.
Audit Action Filter
The abpauditactionfilter is used to integrate the audit log system. It defaults to all actions to record all requests (if the audit is not disabled). You can use audited and disableauditing features to control audit logs for actions and controllers.
Check Filter
The abpvalidationactionfilter is used to integrate the checksum system and automatically checks the input for all actions. In addition, the ABP built-in checksum normalization checks the MVC model.isvalid property and throws a checksum exception if the action's input has any invalid value.
You can use the enablevalidation and disablevalidation features for actions and controllers to control the checksum.
Work Unit Action Filter
Abpuowactionfilter used to integrate the work cell system. It automatically starts a new unit of work before the action executes and completes the unit of work after the action is completed (if no exception is thrown).
You can use the unitofwork feature to control the UOW behavior of the action. You can also use the startup configuration to change the default unit of work properties for all actions.
Exception filter
Abpexceptionfilter used to handle exceptions thrown from the controller actions. It handles and logs the exception, and then returns the wrapped response to the client:
- It only processes object results and does not process view results. Therefore, the actions returned by any object, Jsonresult, or objectresult will be processed. The action returns a view or any other result type that implements the Iactionsresult is not processed. We recommend that you use the Useexceptionhandler extension method built into the Microsoft.AspNetCore.Diagonistics package to handle view exceptions.
- Exception handling and log behavior can be changed for methods or classes using the Wrapresult and Dontwrapresult attributes.
Result Filter
Abpresultfilter is primarily used to wrap the resulting action if the action succeeds.
- It only wraps Jsonresult, Ojectresult, and any objects that do not implement Iactionresult (and the corresponding asynchronous version). If your action returns a view or any other type of result, it will not be wrapped.
- The wrapresult and dontwrapresult attributes can be used for methods and classes to enable/disable wrappers.
- You can use the startup configuration to change the default behavior of the result wrapper .
Result caching for AJAX requests
Abpresultfilter added Cache-control header (No-cache,no-store) for the response of the AJAX request. Therefore, it prevents the browser from caching the response of an AJAX response or even a GET request. You can use configuration to disable this feature.
Model Binder
Abpdatetimemodelbinder Used to standardize DateTime (and nullable<datetime>) inputs, using the clock.normalize method.
View
The MVC view can inherit from the abprazorpage most used infrastructure (Localizationmanager, PermissionChecker, settingmanager...etc) from the dynamic injection. It also has shortcut methods (such as L (...). To localize the text). The startup template inherits it by default.
You can make your Web components inherit from abpviewcomponet instead of viewcomponent to use the benefits of the underlying properties and methods.
Client Agent
The ABP can automatically create JavaScript proxies for all MVC controllers (not just app services). It creates the app service as a controller by default (see section above). You can add a [Remoteservice] attribute to any MVC controller to create a client agent for it. JavaScript proxies are dynamically generated at run time. You need to add the specified script definition to the page:
<src= "~/abpserviceproxies/getall?type=jquery" type= "text/ JavaScript "></script>
Currently, only the jquery proxy is generated. Then we can use JavaScript to invoke an MVC method, as follows:
abp.services.app.product.create ({ ' My test product ', "). Done (function (Result) { //...});
Integration Testing
For ASP. Integration testing is fairly easy, and it has detailed documentation on its own website. The ABP follows this guidance and provides the Abpaspnetcoreintegratedtestbase class in the Abp.AspNetCore.TestBase package. It makes integration testing much easier.
It's a good idea to explore integration testing in the startup template.
Back to home directory
ABP Official document Translation 6.2.1 ASP.