无法从视图访问控制器

时间:2019-06-17 15:19:36

标签: c# asp.net-core controller

我将SPA页面用作视图,并尝试向我的控制器发送请求。我有200个代码,但没有来自控制器的数据。我想路由存在一些问题,或者我请求的URL错误。

我以为

'localhost:5001 / api / game', 'localhost:5001 / api / game / get'

应返回Json值以查看,但它会返回spa索引页面。

Controller.cs

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace Project.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class GameController : Controller
    {
        [HttpGet("[action]")]
        public async Task<IActionResult> Get()
        {
            return Json(new {test = "123"});
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Project
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
    }
}

Startup.cs

namespace project
{
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddDbContext<ProjectDbContext>(options => options.UseNpgsql(Configuration.
            GetConnectionString("project_db")));
        services.AddCors();
        services.AddSpaStaticFiles(config => config.RootPath = "Project_front/build");
    }

    // 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
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseStaticFiles();
        app.UseSpaStaticFiles();
        app.UseCors(builder => {
            builder.AllowAnyHeader();
            builder.AllowAnyMethod();
            builder.AllowAnyOrigin();
        });

        app.UseSpa(spa => {
            spa.Options.SourcePath = "project_front";
            if(env.IsDevelopment()){
                spa.UseReactDevelopmentServer(npmScript:"start");
            }
        });

        app.UseHttpsRedirection();
        app.UseMvc();
    }
 }
}

1 个答案:

答案 0 :(得分:0)

最大的问题是您在调用app.UseMvc();之后使用UseSpa()

UseSpa()实际上是一个包罗万象的中间件,正如dos描述的那样:

  

通过返回单页应用程序(SPA)的默认页面来处理中间件链中此刻的所有请求。

     

此中间件应放在链的后面,以便其他用于服务静态文件,MVC操作等的中间件优先。

简而言之,切勿在{{1​​}} 之前启用此中间件。 中间件的顺序很重要

此外,在配置MVC时映射路由也很好:

UseMvc