路由到剃刀页面应用程序中的 ApiController?
Routing to an ApiController in a razor page app?
我创建了一个 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 页面。
我在启动过程中遗漏了什么吗class?
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 class:
[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 控制器 class。
所以代替:
[Route("api/[controller]")]
[ApiController]
class DomainController : ControllerBase
{
[...]
}
你应该有这个:
[Route("api/[controller]")]
[ApiController]
public class DomainController : ControllerBase // <-- add a public keyword
{
[...]
}
只需将 endpoints.MapControllers() 添加到 UseEndpoints 选项:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
我创建了一个 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 页面。
我在启动过程中遗漏了什么吗class?
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 class:
[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 控制器 class。
所以代替:
[Route("api/[controller]")]
[ApiController]
class DomainController : ControllerBase
{
[...]
}
你应该有这个:
[Route("api/[controller]")]
[ApiController]
public class DomainController : ControllerBase // <-- add a public keyword
{
[...]
}
只需将 endpoints.MapControllers() 添加到 UseEndpoints 选项:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});