Asp.net WebApi .net核心请求返回404

时间:2017-06-13 07:50:07

标签: mongodb asp.net-web-api-routing

我是Asp.net(.Net Core)Web API的新手。 我做了AdminController,但是当我请求URL localhost:52054 / API / admin / get时,我在fiddler上找不到404。 这是我的AdminController& Startup.cs AdminController Image

    namespace Api2017_1.Controllers
{
    [Produces("application/json")]
    [Route("api/Admin")]
    public class AdminController : Controller
    {
        // GET: api/Admin
        [HttpGet]
        public async Task<BsonDocument> Get()
        {
            const string cs = "mongodb://localhost:27017";
            var client = new MongoClient(cs);
            var db = client.GetDatabase("store");
            var coll = db.GetCollection<BsonDocument>("admins");
            using (var cursor = await coll.Find(new BsonDocument()).ToCursorAsync())
            {
                while (await cursor.MoveNextAsync())
                {
                    foreach(var doc in cursor.Current)
                    {
                        return doc;
                    }
                }

            }
            return (BsonDocument)0;
        }

Startup.cs Image

    public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseMvc();
    }
}

任何人都可以指导我错误的地方。 在此先感谢。

1 个答案:

答案 0 :(得分:0)

您要求的网址不正确。使用 localhost:52054 / API / admin 代替请求类型为Get。