我应该将IActionResult与Produces或ActionResult <>一起用于View()吗?

时间:2018-07-10 11:13:13

标签: asp.net-mvc

在我的控制器中,我有始终生成View()的方法。我想将返回类型标记为api Explorer(swagger)。在asp.net中这样做的正确方法是什么?

public class ProductController {

     [Produces(typeof(MyType))]
     public IActionResult MethodA() { ... }

     public ActionResult<MyType> MethodB() { ... }
}

1 个答案:

答案 0 :(得分:0)

是的,可以看看我的解决方案:

我在评论中向您解释了一些非常重要的事情,请不要忘记阅读。

控制器:

// Declare the ProductService Interface
// ProductService contains all of methods you need to get products information
public class ProductController : Controller
{

    private readonly ProductService _productService;

    // Actions
    public ActionResult Index()
    {
        var res = new SearchResult<Product>(); // *Product* is a Class in Model that contains all the variables that you need (product properties)
        return View(res);
    }

    public ActionResult GetProducts(DataSourceRequest request)
    {
        var result = _productService.GetProducts(request); // GetProducts is a method in ProductService
        return Json(result); // you should build your Json swagger
    }
}

型号:

Product.cs:

public string productId { get; set; }
public string productName { get; set; }
public string productPrice { get; set; }
public bool productAvailability { get; set; }

ProductService.cs:

public partial class ProductService : IProductService // IProductService is an interface of IProductService, i will defined it after  ProductService
{
    // To get list of products :
    public List<Product> GetProducts()
    {
        // Initialize new List of products
        List<Product> products= new List<Product>();

        // Your Entity hier
        EntityContext ctx = Enter_youre_Entity_Hier();

        IQueryable<ProductView> query = ctx.ProductViewSet; // At the bottom of the solution you'll find what it is ProductViewSet and how you create one

        return products;
    }

    // Search result
    public SearchResult<Product> GetProducts()
    {

    SearchResult<Product> res;
    EntityContext ctx = Enter_youre_Entity_Hier();
    IQueryable<ProductView> query = 
    DataSourceRequest.BuildLinqQueryFromRequest(Ctx.ProductViewSet);

    query = query.Select(p => p);

    var productSort = query.ToList();

    res = new SearchResult<Product>(
    productSort.ConvertAll<Product>(p => MapProductViewToProduct(p))); // MapProductViewToProduct to mapping the data between Database table "ProductView" to tour local variables in "Product" model

    return res;
    }
    // Mapping data between ProductView and Product
    public Product MapProductViewToProduct(ProductView productView )
    {
    Product product = new Product ();
    product.productId = productView.productId;
    product.productName = productView.productName;
    product.productPrice = productView.productPrice;
    product.productAvailability = productView.productAvailability;

    return product;

    }

}

IProductService.cs:

您必须定义ProductService的接口。

public interface IProductService
{
    SearchResult<Product> GetProducts();
}

什么是ProductView?

SQL中的第一个视图是基于SQL语句结果集的虚拟表。视图包含行和列,就像真实表一样。视图中的字段是数据库中一个或多个实际表中的字段。

创建ProductView:

CREATE VIEW ProductView AS
SELECT productId,
       productName,
       productPrice,
       productAvailability
FROM table_name

然后,您必须更新实体框架以使用ProductView。