我有一個名為“OdeToFood”的小型 ASP.NET Core Web 應用程式專案。開發環境包括:
- IDE:Windows 10 PC 上的 Visual Studio 2019
- ASP.NET 核心 3.1
- 使用 Dapper 從 SQL DB 訪問資料
- 不使用MVC
在其中一個網頁中,將使用 jQuery .ajax 從 DB 檢索記錄。我添加了一個型別為“具有讀/寫操作的 API 控制器”的 ApiController,因為該專案中未使用 EF。
這是 VS 自動生成的代碼(未進行任何更改)。
namespace OdeToFood.Api
{
[Route("api/[controller]")]
[ApiController]
public class RestaurantsController : ControllerBase
{
// GET: api/<RestaurantsController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<RestaurantsController>/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
// POST api/<RestaurantsController>
[HttpPost]
public void Post([FromBody] string value)
{
}
// PUT api/<RestaurantsController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}
// DELETE api/<RestaurantsController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}
我嘗試從瀏覽器中使用以下 URL 對其進行測驗:
- https://localhost:44361/api/Restaurants
- https://localhost:44361/api/Restaurants/8
- https://localhost:44361/api/RestaurantsController
- https://localhost:44361/api/RestaurantsController/8
他們都因 HTTP 404 錯誤而失敗。
由于上面的代碼已經在使用 [Route ...] 的“屬性路由”,我認為前 2 個 URL 的 int 測驗應該可以作業。但他們沒有。我不知道為什么。任何幫助或建議都會很大贊賞。
在 startup.cs 檔案中,configure 部分具有以下設定:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// 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.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
uj5u.com熱心網友回復:
在您的 startup.cs 中嘗試以下
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
在下方評論;
endpoints.MapRazorPages();
uj5u.com熱心網友回復:
Your Scenario:
雖然解決方案非常簡單,但我覺得你有更多的解釋可以擺脫任何進一步的混亂。
正如你所說"Visual Studio 2019",如果你已經接受了asp.net core web api專案,它將weather forecast api controller使用默認值創建,startup.cs如下所示:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
所以如果你直接運行這個專案,它應該default action像下面這樣運行:
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
所以現在來看看你的專案
configuration,它包含如下:app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); });意味著它正在尋找
simple mvc razor page,當您嘗試點擊其中任何一個時,URLs1. https://localhost:44361/api/Restaurants or 2.https://localhost:44361/api/Restaurants/8它應該會得到,404因為它沒有相應地路由。所以你routing template是不正確的route api controller直接。endpoints.MapRazorPages()通常用于Razor mvc page routing.
Solution:
如果您可以API controller在服用時添加,RestaurantsController我認為它看起來像您發布的內容。如果您替換以下內容,則請求應按預期路由service
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
Note:override如果需要,您甚至可以使用路線模板。如果你想要 which a , 你也可以我嘗試向您展示的是誤導您并使您的想法更加敏銳的東西。如果您有任何進一步的疑問,請在此處查看我們的官方檔案。希望它會相應地幫助你。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/397531.html
標籤:C# asp.net核心 网址路由 asp.net-apicontroller

