【发布时间】:2019-04-13 15:52:32
【问题描述】:
我按照excellent ASP.NET tutorial 的说明进行操作,但尝试了几个小时后我根本无法让它工作。
代码没有问题,但是,要么服务器没有根据 Postman 响应,要么存在未触发的运行时异常。我必须承认我对 ASP.NET 完全陌生,所以我真的不知道还能做什么。
为方便起见,我在Github 上进行了公开回购。提前感谢您的帮助。
StartUp.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using ToDoListWebApi.Services;
namespace ToDoListWebApi
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_1);
services.AddSingleton<IIventoryServices, InventoryServices>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
}
}
}
InventoryController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ToDoListWebApi.Models;
using ToDoListWebApi.Services;
namespace ToDoListWebApi.Controllers
{
[Route("v1/")]
[ApiController]
public class InventoryController : ControllerBase
{
private readonly InventoryServices _services;
public InventoryController(InventoryServices services)
{
_services = services;
}
[HttpPost]
[Route("AddInventoryItems")]
public ActionResult<InventoryItems> AddInventoryItems(InventoryItems items)
{
var inventoryItems = _services.AddInventoryItems(items);
if (inventoryItems == null)
{
return NotFound();
}
return inventoryItems;
}
[HttpGet]
[Route("GetInventoryItems")]
public ActionResult<Dictionary<string,InventoryItems>> GetInventoryItems()
{
var inventoryItems = _services.GetInventoryItems();
if (inventoryItems.Count == 0)
{
return NotFound();
}
return inventoryItems;
}
}
}
InventoryItems.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace ToDoListWebApi.Models
{
public class InventoryItems
{
public int Id { get; set; }
public string ItemName { get; set; }
public double Price { get; set; }
}
}
IInventoryServices.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ToDoListWebApi.Models;
namespace ToDoListWebApi.Services
{
public interface IIventoryServices
{
InventoryItems AddInventoryItems(InventoryItems items);
Dictionary<string, InventoryItems> GetInventoryItems();
}
}
InventoryServices.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ToDoListWebApi.Models;
namespace ToDoListWebApi.Services
{
public class InventoryServices : IIventoryServices
{
private readonly Dictionary<string, InventoryItems> _inventoryItems;
public InventoryServices()
{
_inventoryItems = new Dictionary<string, InventoryItems>();
}
public InventoryItems AddInventoryItems(InventoryItems items)
{
_inventoryItems.Add(items.ItemName, items);
return items;
}
public Dictionary<string, InventoryItems> GetInventoryItems()
{
return _inventoryItems;
}
}
}
urlhttps://localhost:port/v1/AddInventoryItems的json查询参数
{
"Id": "1",
"ItemName": "Weed Eater",
"Price": 430
}
【问题讨论】:
-
@bolkay 是不是因为我的帖子没有包含任何代码?
-
是的。如果您认为代码库的“特定”方面是问题的原因,那么您(我认为)更有可能解决您的问题。
-
@bolkay 感谢您的建议,我现在添加了一些代码,希望它可以让每个人都清楚。我不知道从哪里开始寻找答案。
-
您似乎已经完全按照他的教程进行操作,但他删除了这段代码,app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); });
-
@Peter 刚试过,没用。感谢您注意到代码中的细微差别
标签: c# asp.net asp.net-mvc postman