学习009-08-01Access Object Space, Security System, and Caption Helper in Custom Endpoint Methods(在自定义端

Access Object Space, Security System, and Caption Helper in Custom Endpoint Methods(在自定义端点方法中访问对象空间、安全系统和标题助手 )

This topic describes how to access an Object Space, the Security System, and the Caption Helper from custom endpoint methods.
本主题介绍如何从自定义端点方法访问对象空间、安全系统和标题助手。
在这里插入图片描述

Prerequisites(先决条件)

If you created your application in v21.2 or earlier, you need to use the Application Builder’s ObjectSpaceProviders property to register Object Space Providers instead of the XafApplication.CreateDefaultObjectSpaceProvider method or XafApplication.CreateCustomObjectSpaceProvider event. Refer to the following help topic for more information on how to do this: Integrate Application Builders into Existing Applications.
如果您在v21.2或更早版本中创建了应用程序,则需要使用Application Builder的ObjectSpaceProviders属性来注册对象空间提供程序,而不是XafApplication.CreateDefaultObjectSpaceProvider方法或XafApplication.CreateCustomObjectSpaceProvider事件。有关如何执行此操作的更多信息,请参阅以下帮助主题:将Application Builders集成到现有应用程序中。

We strongly recommend this because the use of the XafApplication.CreateDefaultObjectSpaceProvider method and XafApplication.CreateCustomObjectSpaceProvider event conflicts with IObjectSpaceFactory and IObjectSpaceProviderFactory service usage.
我们强烈建议这样做,因为使用XafApplication.CreateDefaultObjectSpaceProvider方法和XafApplication.CreateCustomObjectSpaceProvider事件与IObjectSpaceFactory和IObjectSpaceProviderFactory服务使用冲突。

The use of IObjectSpaceFactory instead of XafApplication’s members also improves the performance of your application because the application instance is not created in this case.
使用IObjectSpaceFactory而不是XafApplication的成员也可以提高应用程序的性能,因为在这种情况下不会创建应用程序实例。

Examples(例子)

Object Space(对象空间)

To create an Object Space in an ASP.NET Core application, inject the IObjectSpaceFactory service. Note that this service ensures if a user is logged on. If not, it throws an authorization exception. To avoid this exception, you can use the following techniques:
要在ASP.NETCore应用程序中创建对象空间,请注入IObjectSpaceFactory服务。请注意,此服务可确保用户是否已登录。如果未登录,则会抛出授权异常。要避免此异常,您

  • 可以使用以下技术:
    Use the INonSecuredObjectSpaceFactory service instead of IObjectSpaceFactory.
    使用INonSecuredObjectSpaceFactory服务代替IObjectSpaceFactory。
  • Use another way to ensure if a user is logged on (for example, use AuthorizationPolicyBuilder.RequireXafAuthentication with
    AuthorizeAttribute).

    使用另一种方法来确保用户是否已登录(例如,使用AuthorizationPolicyBuilder.RequireXafAuthentication和AuthorizeAtcade)。

C#

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;

namespace MySolution.WebApi {
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class CustomEndpointController : ControllerBase {
        IObjectSpaceFactory objectSpaceFactory;
        public CustomEndpointController(IObjectSpaceFactory objectSpaceFactory) {
            this.objectSpaceFactory = objectSpaceFactory;
        }
        [HttpGet]
        public IActionResult Get() {
            using IObjectSpace newObjectSpace = objectSpaceFactory.CreateObjectSpace<Contact>();
            // ...
            return Ok();
        }
        //...
    }
}

Security System(安全系统)

To access the Security System in an ASP.NET Core application, inject the ISecurityProvider service. Note that this service ensures if a user is logged on. If not, it throws an authorization exception. To avoid this exception, you can use the following techniques:
要访问ASP.NET核心应用程序中的安全系统,请注入ISecurityProvider服务。请注意,此服务可确保用户是否已登录。如果未登录,则抛出授权异常。要避免此异常,您可以使用以下技术:

  • Use the ISecurityStrategyBase service instead of ISecurityProvider if you do not need to operate with an authenticated user object. This service does not ensure if a user is logged on or not, so the current user object might not be available here.
  • 如果您不需要使用经过身份验证的用户对象进行操作,请使用ISecurityStrategyBase服务而不是ISecurityProvider。此服务不确保用户是否已登录,因此当前用户对象可能在此处不可用。
  • Use another way to ensure if a user is logged on (for example, use AuthorizationPolicyBuilder.RequireXafAuthentication with AuthorizeAttribute.
  • 使用另一种方法来确保用户是否已登录(例如,使用AuthorizationPolicyBuilder。RequireXafAuthentication与Authorize属性。

These workarounds do not guarantee that you will not receive authentication exceptions, even if you specify the correct user credentials.
即使您指定了正确的用户凭据,这些解决方法也不能保证您不会收到身份验证异常。

C#

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using MySolution.Module.BusinessObjects;

namespace MySolution.WebApi {
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class CustomEndpointController : ControllerBase {
        ISecurityProvider securityProvider;
        public CustomEndpointController(ISecurityProvider securityProvider) {
            this.securityProvider = securityProvider;
        }
        [HttpGet]
        public IActionResult Get() {
            ISecurityStrategyBase security = securityProvider.GetSecurity();
            var userId = security.UserId;
            // ...
            return Ok();
        }
        //...
    }
}

Caption Helper(标题助手)

The Caption Helper class allows you to get localized captions for XAF Controllers and Razor Components that are shown in XAF Views. XAF initializes the Application Model of this class on requests to standard XAF pages. To get captions with other requests (middleware, Web API method, and so on), use the ICaptionHelperProvider service.
Caption Helper类允许您获取XAF视图中显示的XAF控制器和Razor组件的本地化字幕。XAF根据对标准XAF页面的请求初始化此类的应用程序模型。要获取其他请求(中间件、Web API方法等)的字幕,请使用ICaptionHelperProvider服务。

This service uses a shared Application Model and does not return user-specific localized strings from a user Model Differences Storage. If your application does not store different captions for different users in the Application Model, use the ICaptionHelperProvider service as a unified way to get localized captions.
此服务使用共享的应用程序模型,不会从用户模型差异存储中返回用户特定的本地化字符串。如果您的应用程序没有在应用程序模型中为不同用户存储不同的字幕,请使用ICaptionHelperProvider服务作为统一的方式来获取本地化字幕。

C#

using DevExpress.ExpressApp.AspNetCore.Services.Localization;
using DevExpress.ExpressApp.Utils;
using Microsoft.AspNetCore.Mvc;


namespace MySolutionName.Module.Blazor.Controllers {
    public class CustomLocalizationController : ControllerBase {
        internal ICaptionHelperProvider captionHelperProvider;


        public CustomLocalizationController(ICaptionHelperProvider captionHelperProvider) {
            this.captionHelperProvider = captionHelperProvider;
        }


        [HttpGet]
        public string GetActionCaption(string actionName) {
            ICaptionHelper helper = captionHelperProvider.GetCaptionHelper();
            return helper.GetActionCaption(actionName);
        }
    }
}

The CaptionHelper.GetService method returns an ICaptionHelper instance to use on other platforms such as Win and Web.
CaptionHelper. GetService方法返回一个ICaptionHelper实例以在Win和Web等其他平台上使用。

C#

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Utils;


ICaptionHelper helper = CaptionHelper.GetService(Application.ServiceProvider)
string newActionName = helper.GetActionCaption("New");
// ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值