来自代码的web-api post对象 [英] web-api post object from code behind

查看:130
本文介绍了来自代码的web-api post对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是非常新的mvc web api



我创建了一个web api Post方法,它接受一个对象类型Bag并返回一个HTMLString,代码是如下所示



i am very new to mvc web api

I have crated a web api Post method which takes an object type "Bag" and return a HTMLString the code is as shown bellow

public HtmlString PostBag(Bag bagofItem)
    {
       return  Utility.PostBagDiscountedItem(bagofItem);
    }





现在来自我的网站我想从控制器调用API方法PostBag PostBag()



如果有人能告诉我怎么做,我不知道如何理解



什么我的网络应用程序如下所示





now from my web site i wanted to call the API method PostBag from the controller PostBag()

and i am do not know how to and appreciate if some one can show me how to do this

what i have got in my web application is as bellow

public class HomeController : Controller
{
    private Bag _bag = new Bag();
    private string uri = "http://localhost:54460/";

 public ActionResult PostBag()
    {


          // would some one show me how to POST the _bag to API Method PostBag()

        return View();
    }










public class Bag 
{
    private static  List<Product> _bag { get; set; }

    public List<Product> GetBag ()
    {
        if (_bag == null)
            _bag = new List<Product>();
       return _bag;
    }



}



感谢您的所有帮助



感谢


appreciate all your help

thank

推荐答案

您可以在WebApi中发布数据,如下所示

You can post data in WebApi like below
public class Bag
{
public string Name{get;set;}
}
var dataJSON = { bags: [{ Name: "Bag1", Name: "Bag2" }, 
                            { Name: "Bag4", Name: "Bag3" }] };


.ajax({
type :' POST'
url:' 在此指定网址'
数据:JSON.stringify(dataJSON),
contentType:' application / json; charset = utf-8'
dataType:' json'
});
.ajax({ type: 'POST', url: 'specify the url here', data: JSON.stringify(dataJSON), contentType: 'application/json; charset=utf-8', dataType: 'json' });










public void Post(List<Bag> bags)
{
}



希望这有助于


Hope this helps


这篇关于来自代码的web-api post对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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