返回

c#-如何在中注册wep api控制器?净核心6?

发布时间:2022-02-23 00:30:57 357

I have created a small app in .net core 6.

以下是项目结构。

project
│   
└───wwwroot
│   │   Content 
└───Api
    │   ResturantsController
    │   

现在我有了ResturantsController

这是课程。

namespace OdeToFood.Api
{
    [Route("api/RestaurantsController")]
    [ApiController]
    public class RestaurantsController : ControllerBase
    {

        [Route("api/Restaurants")]
        [HttpGet]
        public IEnumerable Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
}

在我的节目里

我有以下几点:

using OdeToFood.Data;
using Microsoft.EntityFrameworkCore;
// required for command line to tell ef that my startp project is here;
using Microsoft.EntityFrameworkCore.Design;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddScoped<IRestaurantData,SqlRestaurantData>();
// EF
builder.Services.AddDbContextPool(Options =>
 {
     Options.UseSqlServer(builder.Configuration.GetConnectionString("OdeToFoodDb"));
 });
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
// Api controllers
app.MapControllers();
app.MapDefaultControllerRoute();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();

我试图使用以下端点访问apihttps://localhost:7285/api/Restaurants

I am getting the 404.

有什么原因吗?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像
相关帖子