在VS 2010的Web应用程序ASP.NET网页API CRUD操作 [英] ASP.NET Web Api CRUD operation in VS 2010 web application

查看:103
本文介绍了在VS 2010的Web应用程序ASP.NET网页API CRUD操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让2010 Web应用程序在VS ASP.NET网页API CRUD操作,但为什么结果不是从源表返回所有整行。

I tried to make ASP.NET Web Api CRUD operation in VS 2010 web application, but why the result is not returning all entire row from source table.

这是我的code:

路由/ Globax.asax

Route/Globax.asax

protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapHttpRoute(
            name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}", // browse with localhost:7031/api/product
                //routeTemplate: "{controller}/{id}",  // browse with localhost:7031/product
                 defaults: new { id = System.Web.Http.RouteParameter.Optional }
      );

控制器/ ProductController.cs:

Controller/ProductController.cs :

    public class ProductController : ApiController
    {
        NorthwindEntities db = new NorthwindEntities();

        public List<Product> GetAll()
        {
            return db.Products.ToList<Product>();// ;
        }

查看/ ViewProduct.aspx:

View/ViewProduct.aspx :

    <script src="Script/jquery-1.7.1.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(function () {
        $('#<%= cviewproduct.ClientID %>').click(function (e) {
            getProducts();
            e.preventDefault();
        });

    });

    function getProducts() {
        $.getJSON("/api/product",
                function (data) {
                    $.each(data, function (key, val) {
                        //var str = val.ProductName;
                        // alert(str);

                        var row = '<tr> <td>' + val.ProductName + '</td><td>' + val.ProductID + '</td><tr/>';

                        $(row).appendTo($('#tblproduct'));


                    });
                });
    }
</script>

·贝娄是产品控制器的结果通过的http://本地主机:7031 / API /产品的:

Bellow Is The Result of product Controller via 'http://localhost:7031/api/product' :

贝娄的getProducts的)结果(功能:

Bellow Is The Result of getProducts() function :

请帮我。

任何想法或建议?

推荐答案

在执行 $。的getJSON(/ API /产品,... ,你是当你发布你正重返JSON没有得到回XML。

When you execute $.getJSON("/api/product", ..., you are not getting back XML as you posted. You are getting back JSON.

作为第一步,我建议你下载并安装 Fiddler2 。打开它,并使用作曲家标签来执行的 HTTP的GET://本地主机:7031 / API /产品。这应该告诉你的JSON返回,这将是从XML不同。张贴JSON你原来的问题,我们应该能够进一步帮助。

As a first step, I suggest you download and install Fiddler2. Open it, and use the Composer tab to execute a GET for http://localhost:7031/api/product. That should show you the JSON returned, which will be different from the XML. Post that JSON to your original question and we should be able to help further.

我的猜测是JSON格式不正确。什么是您的WebApiConfig.cs类的样子?

My guess is that the JSON is not properly formatted. What does your WebApiConfig.cs class look like?

更新

是的,有一个更好的办法。首先,创建一个ApiModel(ApiModel是什么的WebAPI视图模型是MVC):

Yes, there is a better way. First, create an ApiModel (ApiModel is to WebAPI what ViewModel is to MVC):

public class ProductApiModel
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
}

现在,实体从控制器返回这个代替:

Now, return this instead of an entity from your controller:

public class ProductController : ApiController
{
    public IEnumerable<ProductApiModel> GetAll()
    {
        var products = db.Products
            .OrderBy(x => x.ProductID)
            .Select(x => new ProductApiModel
            {
                ProductId = x.ProductID,
                ProductName = x.ProductName
            });
        return products;
    }
}

如果您使用 AutoMapper ,你可以让你的控制器codeA有点短:

If you used AutoMapper, you could make your controller code a bit shorter:

public class ProductController : ApiController
{
    public IEnumerable<ProductApiModel> GetAll()
    {
        var entities = db.Products.OrderBy(x => x.ProductID);
        var models = Mapper.Map<ProductApiModel[]>(entities);
        return models;
    }
}

这篇关于在VS 2010的Web应用程序ASP.NET网页API CRUD操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆