无法使此简单的ASP.NET教程代码运行

时间:2018-11-10 06:28:49

标签: c# asp.net asp.net-mvc postman

我遵循了此excellent ASP.NET tutorial的说明,但是尝试了几个小时后,我还是无法使用它。

代码没有任何问题,但是服务器没有按照Postman进行响应,或者有未触发的运行时异常。我必须承认我是ASP.NET的新手,所以我真的不知道该怎么办。

为方便起见,我在Github上进行了公开回购。预先感谢您的帮助。

The project structure

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;
        }
    }
}

URL https://localhost:port/v1/AddInventoryItems的Json查询参数

{
  "Id": "1",
  "ItemName": "Weed Eater",
  "Price": 430
}

邮递员截屏: Postman screenshoot

2 个答案:

答案 0 :(得分:1)

如果我有足够的代表,我会发表评论。您确定IIS正在运行吗?应该在桌面上的隐藏图标工具栏中看到它

答案 1 :(得分:0)

我放弃了本教程,有很多错误不会触发异常,但是如果与本教程from Microsoft进行比较,您可以意识到这不是构建代码的最佳方法。使用ASP.NET的Web Api。无论如何,感谢所有尝试提供帮助的人。