asp.net web api如何以Json格式发布日期? [英] asp.net web api How to Post date in Json format?

查看:67
本文介绍了asp.net web api如何以Json格式发布日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我有这个控制器:(当你使用mvc创建项目时这是默认设置)



Hey guys, I have this controller: (It's the default when you create a project with mvc)

[ResponseType(typeof(Event))]
        public async Task<IHttpActionResult> PostEvent(Event @event)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Events.Add(@event);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = @event.Id }, @event);
        }





我想要做的是能够使用JSON发布/放入数据库任何提示如何这样做?



What I want to do is to able to post/put into the DB using JSON any tips on how to do this?

推荐答案

您将使用存储库模式并在您的控制器中实现它(Mvc或Api)

例如控制器Mvc使用此代码:

************************************ ************************

// POST:/人/创建

[HttpPost ]

[ValidateAntiForgeryToken]

公共异步任务< actionresult>创建(人物)

{

if(ModelState.IsValid)

{

await _genericRepository.InsertAsync(人);

返回RedirectToAction(索引);

}



返回查看(人);

}

********************************* *****************************

小心我使用的文件夹包含两个关于Repository的文件模式如果你想我会发给你我的代码来源

然后你会下载一个邮差Chrome Rest客户端并选择Post方法来创建你想要的东西。
hi ,you will use a repository pattern and implement it in your controller (Mvc or Api)
for example with Controller Mvc use this code :
************************************************************
// POST: /Persons/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<actionresult> Create(Person person)
{
if (ModelState.IsValid)
{
await _genericRepository.InsertAsync(person);
return RedirectToAction("Index");
}

return View(person);
}
**************************************************************
be careful i am used a folder that contains two files concerning Repository pattern if you want i will send you my code source
then you will download a Postman Chrome Rest client and choose Post method to create what you want.


这篇关于asp.net web api如何以Json格式发布日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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