【问题标题】:Asp.net core with Aurelia not showing api route带有 Aurelia 的 Asp.net 核心未显示 api 路由
【发布时间】:2017-09-28 17:11:30
【问题描述】:

我刚刚开始使用 Asp.net core apiAurelia

我通常使用Laravel

我已经安装了所有东西 + aurelia 路由器。现在我想对我的 asp.net 核心 api 进行 api 调用,但我没有看到 json 结果?

示例控制器如下所示:

public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

}

但是当我使用 google chrome http://localhost:9001/api/values 发出获取请求时,我只看到一个空白的白页?我应该如何联系我的api

编辑

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.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Forum
{
    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.UseDefaultFiles(); 
            app.UseStaticFiles();
            app.UseMvc();
        }
    }
}

【问题讨论】:

  • 您使用 google chrome 发出的请求的 HTTP 响应状态如何?
  • 请分享您的 Startup.cs 课程
  • 我没有看到状态码。 @S.Petrosov 请看我的编辑。
  • @Jamie 在任何浏览器开发工具窗口中按 F12 将打开转到网络并找到您的请求会有响应代码,例如像这样 take.ms/CtlcH
  • @S.Petrosov 谢谢我看到了这个:s9.postimg.org/l1stfedhr/…

标签: c# asp.net-mvc asp.net-core aurelia


【解决方案1】:

但是当我使用 google chrome http://localhost:9001/api/values 发出获取请求时,我只看到一个空白的白页?我应该如何联系我的 api?

由于 Configure 定义到 Startup 类中的这一行,您看不到任何内容。

app.UseMvc();

此行只是启用属性路由,不会为您添加任何默认路由。

你有两个选择:

第一个选项:

通过使用此属性[Route("api/values"] 来装饰您的控制器来使用属性路由。

第二个选项:

使用UseMvc 重载,让您按照约定定义路线。因此,在您的 Configure 方法中,只需将 app.UseMvc() 替换为以下代码:

app.UseMvc(routes => routes.MapRoute("default", "api/{controller}/{action}/{id?}");

要了解有关路由的更多信息,请关注此link

【讨论】:

    【解决方案2】:

    添加如下函数:

    private void ConfigureRoutes(IRouteBuilder routeBuilder)
    {
        routeBuilder.MapRoute("Default", "api/{controller=Home}/{action=Index}");
    }
    

    换行

    app.UseMvc();
    

    app.UseMvc(ConfigureRoutes);
    

    现在您可以向“api/Values/Get”网址发送请求了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 2021-09-07
      • 2016-12-30
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多