【问题标题】:web forms and web api integrationWeb 表单和 Web API 集成
【发布时间】:2016-10-04 00:53:39
【问题描述】:

任何人都可以向我提供您对如何进行的意见。以下要求。

我在 VS Professional 2015 中开发了 Web 应用程序,并且有单独的 Web API 应用程序。到目前为止,现有的 Web 应用程序是在 3 层架构中实现的。现在我们想使用没有 3 层架构的 Web API 调用来实现新页面。

首先,我想在我的 Web 应用程序中创建一个基础架构/架构来调用 Web API 应用程序。这样所有的新页面请求都会通过此基础架构/架构并调用 Web API。

请帮助我提出您宝贵的意见/建议。

提前谢谢你。

【问题讨论】:

  • 使用angular js或jquery通过ajax获取数据。服务器初始页面为静态 html 文件。

标签: asp.net-web-api webforms


【解决方案1】:

当我们使用API 时,除非愿意使用ReactJsAngularJs,否则无需创建复杂的基础架构/架构。 它甚至对他们来说也不是强制性的,但它会让你保持代码干净。您只需要使用 JavaScript 调用 API 例如,您有一个 API 控制器方法并希望访问它。

产品的简单方法

#region Get Method
    // GET api/product

    [Queryable]

    [Route("Products")]
    [Route("All")]

    public HttpResponseMessage Get()
    {
        try
        {
            var products = _productServices.GetAllProducts();
            var productEntities = products as List<ProductEntity> ?? products.ToList();
            if (productEntities.Any())
            {
                return Request.CreateResponse(HttpStatusCode.OK, productEntities);
            }

            throw new ApiDataException(1000,"No Products Found",HttpStatusCode.NotFound);
        }
        catch (Exception)
        {
            throw;
        }
    }

    #endregion

只需像这样从 ajax 调用受尊敬的方法

$(document).ready(function () {
var a = "Test";
$.ajax({
    url: "v1/products/product/All",
    type: "GET",
    data: { a : a },
    success: function (response) {
        alert(response);
    },
    error: function (response) {
        alert(response);
    }
});});

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多