简体   繁体   English

使用Angularjs在MVC控制器中访问硬编码数据

[英]Access hard-coded data in MVC controller using Angularjs

I have some knowledge about Angularjs but I'm new to ASP.NET MVC. 我对Angularjs有一些了解,但是我是ASP.NET MVC的新手。 So I was following this https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api tutorial. 所以我一直在关注这个https://docs.microsoft.com/zh-cn/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api教程。 But I came up with a problem as it uses Jquery. 但是我想到了一个问题,因为它使用了Jquery。 I want to access the hard coded data in below controller using Angular. 我想使用Angular访问下面控制器中的硬编码数据。

using ProductsApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;

namespace ProductsApp.Controllers
{
    public class ProductsController : ApiController
    {
        Product[] products = new Product[] 
        { 
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
        };

        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }

        public IHttpActionResult GetProduct(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }
    }
}

I just want the method to access the data. 我只希望该方法访问数据。

Can anyone help me with this. 谁能帮我这个。 Thanks 谢谢

Use $http in your service or controller of AngularJS to access data from your Web API. 在服务或AngularJS控制器中使用$http可以从Web API访问数据。 For example: 例如:

$http.get("http://localhost:8080/api/products/getallproducts")
    .then(function(response) {
        console.log(response.data);
    });

If writing WebApi application then you need to change the IHttpActionResult Name to Get. 如果编写WebApi应用程序,则需要将IHttpActionResult名称更改为Get。 and try this URL: 并尝试以下URL:

localhost:8080/api/products/{id}

*{id} is optional. * {id}是可选的。

if you want to use the route you should change WebApiConfig.cs in App_Start folder. 如果要使用路由,则应在App_Start文件夹中更改WebApiConfig.cs

Take a look at angularjs $http service which is used to establish communication with the http server in situation as yours. 看一下angularjs $ http服务,该服务用于在您的情况下与http服务器建立通信。

To call GetAllProducts() , using the $http , use the route "api/products" like this 要使用$http调用GetAllProducts() ,请使用类似这样的路由"api/products"

$http.get("api/products")
    .then(function(response) {
        console.log(response);
});

And to call GetProduct() use the route "api/products/1" if your product is 1 like this 如果您的产品是1, GetProduct()调用GetProduct()使用路由"api/products/1"

$http.get("api/products/1")
    .then(function(response) {
        console.log(response);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM