【问题标题】:Routing to an ApiController in a razor page app?在剃须刀页面应用程序中路由到 ApiController?
【发布时间】:2018-12-04 12:00:49
【问题描述】:

我创建了一个 ASP.NET Core Razor 页面应用程序(asp.net 版本 2.1.1)。它适用于普通页面,但我也想要本教程中的 ApiController: https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-vsc?view=aspnetcore-2.1

但是,当我像上面的示例一样创建控制器时,每当我尝试访问它时都会得到一个 404 页面。

我在启动课程中缺少什么吗?

public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<DomainDbContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // 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.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc();
        }

还有我的 apicontroller 类:

    [Route("api/[controller]")]
    [ApiController]
    class DomainController : ControllerBase 
    {
        private readonly DomainDbContext _context;

        public DomainController (DomainDbContext context) 
        {
            _context = context;
        }

        [HttpGet]
        public ActionResult<List<Domain>> GetAll()
        {
            return new List<Domain> {new Domain() {Name ="Hello", Tld = ".se", Expiration = DateTime.UtcNow.AddDays(35)}};
        }
}

据我所见,一切看起来都像指南,但显然有些地方不正确,因为我得到了所有页面的 404。即使我创建了一个新方法,它也不能真正按预期工作并且无法访问。

我尝试过的主要路径是/api/domain。

提前感谢您的帮助!

【问题讨论】:

  • 也许是 public 类 DomainController
  • @mate 现在看起来太明显了......谢谢。让它成为一个答案,我会接受它。
  • 新鲜的眼睛 :) @CodeNotFound 已经添加了它
  • @Mate :D 没有看到你的 cmets。我正在添加我的答案并对其进行格式化,而您已经在 20h45 添加了您的评论。我在 20h47 的回答:D
  • @CodeNotFound 重要的是代码现在可以工作;)

标签: c# asp.net-core asp.net-core-webapi razor-pages


【解决方案1】:

你需要有一个公共的控制器类。

所以而不是:

[Route("api/[controller]")]
[ApiController]
class DomainController : ControllerBase
{
    [...]
} 

你应该有这个:

[Route("api/[controller]")]
[ApiController]
public class DomainController : ControllerBase // <-- add a public keyword
{
    [...]
} 

【讨论】:

    【解决方案2】:

    只需将 endpoints.MapControllers() 添加到 UseEndpoints 选项:

    app.UseRouting();
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapRazorPages();
                    endpoints.MapControllers();
                });
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多