【问题标题】:Can't make this simple ASP.NET tutorial code run无法让这个简单的 ASP.NET 教程代码运行
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 是的,IIS 正在运行,我可以看到 Hello World!首页。但是在尝试发布新项目时,它不会触发异常或从中返回答案。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 2016-03-15
    • 2016-01-17
    相关资源
    最近更新 更多