我使用.Net 5.0創建了一個新的ASP.NET Core Razor專案,我創建了一個簡單的Web服務API頁面,如下所示。問題是,當我運行它,然后在瀏覽器中呼叫http://localhost:50050/api/test,我得到 "HTTP ERROR 404*",意思是找不到我的網路服務。我有一個類似的應用程式,只是用老版本的.net core做的,而且作業得很好。 有誰知道我做錯了什么嗎?
usingMicrosoft.AspNetCore.Mvc;
namespace WebApplication2.Api
{
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
[HttpGet] 。
public int GetTest()
{
return 21;
}
[HttpGet("{id}"/span>)]
public int GetTest([FromRoute] int id)?
{
return 22。
}
}
啟動頁面如下:
usingMicrosoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
使用System.Collections.Generic.Details; 使用System.Collections.Generic.Details
using System.Threading.Tasks;
namespace WebApplication2
{
public class Startup {
{
public Startup(IConfiguration configuration)
{
配置 = 配置。
}
public IConfiguration 配置 { get; }
//此方法被運行時呼叫。使用此方法來向容器添加服務。
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages()。
}
//此方法被運行時呼叫。使用此方法來配置HTTP請求管道。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage()。
}
else (env.IsDevelopment())
{
app.UseExceptionHandler("/Error"/span>)。
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages()。
});
}
}
}
下面是我的專案布局
uj5u.com熱心網友回復:
你只有一個端點到你的RazorPages,而不是與你的控制器。
app.UseEndpoints(endpoints => //span>
{
endpoints.MapRazorPages()。
});
你需要做的是在Startup ConfigureServices方法中注冊你的控制器:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()。
然后在你的端點中間件中添加這些控制器,像這樣:
public void Configure(IApplicationBuilder app) {
//其他中間件。
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages()。
endpoints.MapControllers()。
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/328670.html
標籤:
上一篇:為什么JsonResult需要同時實作IActionResult和IStatusCodeActionResult?
下一篇:通過代理驗證IstioToken
