asp.net core WebApi 快速入门

目录(?)[+]

 

参考:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.1

官网的例子

直接去看,这里写用到的代码

创建一个model

namespace TodoApi.Models
{
    public class TodoItem { public long Id { get; set; } public string Name { get; set; } public bool IsComplete { get; set; } } }

创建数据库上下文

using Microsoft.EntityFrameworkCore;

namespace TodoApi.Models
{
    public class TodoContext : DbContext { public TodoContext(DbContextOptions<TodoContext> options) : base(options) { } public DbSet<TodoItem> TodoItems { get; set; } } }

Register the database context

 public void ConfigureServices(IServiceCollection services) { services.AddDbContext<TodoContext>(opt => opt.UseInMemoryDatabase("TodoList")); services.AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }

添加控制器 TodoController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using TodoApi.Models; namespace TodoApi.Controllers { [Route("api/[controller]")] [ApiController] [EnableCors("any")] public class TodoController : ControllerBase { private readonly TodoContext _context; public TodoController(TodoContext context) { _context = context; if (_context.TodoItems.Count() == 0) { // Create a new TodoItem if collection is empty, // which means you can't delete all TodoItems. _context.TodoItems.Add(new TodoItem { Name = "Item1" }); _context.SaveChanges(); } } // GET api/todo [HttpGet] public ActionResult<List<TodoItem>> GetAll() { return _context.TodoItems.ToList(); } // GET api/todo/{id} [HttpGet("{id}", Name = "GetTodo")] public ActionResult<TodoItem> GetById(long id) { var item = _context.TodoItems.Find(id); if (item == null) { return NotFound(); } return item; } //POST /api/todo/ //Postman: Body->Raw->Json-> {"name":"Item2~","isComplete":false} //Header: Location →http://localhost:64425/api/Todo/2 [HttpPost] public ActionResult Create(TodoItem item) { _context.TodoItems.Add(item); _context.SaveChanges(); return CreatedAtRoute("GetTodo", new { id = item.Id }, item); } [HttpPut("{id}")] public ActionResult Update(long id, TodoItem item) { var todo = _context.TodoItems.Find(id); if (todo == null) { return NotFound(); } todo.IsComplete = item.IsComplete; todo.Name = item.Name; _context.TodoItems.Update(todo); _context.SaveChanges(); return NoContent(); } [HttpDelete("{id}")] public IActionResult Delete(long id) { var todo = _context.TodoItems.Find(id); if (todo == null) { return NotFound(); } _context.TodoItems.Remove(todo); _context.SaveChanges(); return NoContent(); } } }

然后可以调用了。上面代码已经设置跨域 Microsoft.AspNetCore.Cors;

跨域

1.全局配置中启用跨域处理,命名为‘any’,任何都可以访问

public void ConfigureServices(IServiceCollection services) { //配置跨域处理 services.AddCors(options => { options.AddPolicy("any", builder => { builder.AllowAnyOrigin() //允许任何来源的主机访问 .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials();//指定处理cookie }); }); }

2.在控制器或Action的方法注释上使用对应名称的 跨域规则,
[EnableCors("any")]

    [Produces("application/json")]
    [Route("api/Menu")] [EnableCors("any")] //设置跨域处理的 代理 public class MenuController : Controller { }

cookie

1.后台通过HttpContext上下文可以直接操作Cookie
2.前台JQuery的ajax请求,需要携带withCredentials才会将cookie的值保存到客户端

跨域:https://www.cnblogs.com/tianma3798/p/6920704.html

 

转载于:https://www.cnblogs.com/webenh/p/11434720.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值