Umbraco,在自定义 Api 控制器中调用操作

问题描述 投票:0回答:1

我需要知道如何在自定义 Api 控制器中调用操作。例如,在本官方指南中,有一个名为“BlogsCommentController”的控制器。执行“GetComments”操作... https://docs.umbraco.com/umbraco-cms/tutorials/getting-started-with-entity-framework-core

在代码的其他部分,服务或其他方法如何调用具有以下网址的操作:“Umbraco/Api/BlogComments/GetComments”并返回评论列表?

我想知道如何调用控制器(Api 控制器),更准确地说是其返回请求值的操作。

提前致谢。

c# asp.net-mvc asp.net-core umbraco
1个回答
0
投票

这些是我创建自定义服务的步骤

1-创建界面

    public interface IProductService
{
    IEnumerable<string> GetAllProducts();
    string GetProduct(int? id);
}

2-创建服务

 public class ProductService : IProductService
    {
        public IEnumerable<string> GetAllProducts()
        {
            return new[] { "Table", "Chair", "Desk", "Computer" };
        }

        // [Route("product/{id?}")]
        //https://localhost:44326/Umbraco/AwesomeProducts/Products/GetProduct?id=5
        public string GetProduct(int? id)
        {
            if (id is not null)
            {
                return $"Monitor model {id}";
            }
            return "Base model Monitor";
        }
    }

3-创建控制器

[PluginController("AwesomeProducts")] 公共类 ProductsController : UmbracoApiController { 私有只读 IProductService _productService; 公共产品控制器(IProductService 产品服务) { _产品服务=产品服务; } 公共 IEnumerable GetAllProducts() { 返回 _productService.GetAllProducts(); }

// [Route("product/{id?}")]
//https://localhost:44326/Umbraco/AwesomeProducts/Products/GetProduct?id=5
public string GetProduct(int? id)
{
  return _productService.GetProduct(id);
}

}

4-创建扩展

   public static class UmbracoBuilderServiceExtensions
    {
        public static IUmbracoBuilder AddCustomServices(this IUmbracoBuilder builder)
        {
            builder.Services.AddScoped<ICustomNewsArticleService, CustomNewsArticleService>();
            builder.Services.AddSingleton<IProductService, ProductService>();

            return builder;
        }
    }

5-修改启动方式

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddUmbraco(_env, _config)
                .AddBackOffice()
                .AddWebsite()
                .AddComposers()
                // Register all Delivery API dependencies
                .AddDeliveryApi()
                .AddCustomServices()
                .Build();

            services.AddControllers().AddJsonOptions(
        Constants.JsonOptionsNames.DeliveryApi,
        options =>
        {
            // set the maximum allowed depth of
            options.JsonSerializerOptions.MaxDepth = 200;
        });
        }

6-调用API //https://localhost:44326/Umbraco/AwesomeProducts/Products/GetProduct?id=5

7-从剃须刀页面调用

@inject IProductService productService

    @{
    Layout = "master.cshtml";
    bool isPageProtected = PublicAccessService.IsProtected(Model.Path);
    var prodList = productService.GetAllProducts();

}
@if (isPageProtected)
{
        <h1>Secret Page - shhshshsh!</h1>
}
else
{
    <h1> Get Data From Controller</h1>
    <ul>
       
    @foreach (var item in prodList)
    {
       <li>@item</li> 
    }
    </ul>
}
© www.soinside.com 2019 - 2024. All rights reserved.