當我想在我的 Mac 上使用 .Net Core MVC 架構和 Visual Studio 2019 程式運行我的專案時,我收到錯誤“找不到此本地主機頁面”。我正在共享 Startup.cs 和控制器類。
我正在使用 .NetCore 3.1 版。
提前致謝。

namespace Test
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSingleton<VendorRegistrationService>();
services.AddCors(o => o.AddPolicy("ReactPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
//.AllowCredentials();
}));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("ReactPolicy");
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
VendorRegistrationController.cs
namespace Test.Controllers
{
[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
[EnableCors("ReactPolicy")]
public class VendorRegistrationController : ControllerBase
{
public readonly VendorRegistrationService vendorRegistrationService;
public VendorRegistrationController(VendorRegistrationService vendorRegistrationService)
{
this.vendorRegistrationService = vendorRegistrationService;
}
[HttpPost]
public IActionResult Post([FromBody] VendorRegistration vendorRegistration)
{
return CreatedAtAction("Get", vendorRegistrationService.Create(vendorRegistration));
}
}
}
uj5u.com熱心網友回復:
這是一個 web api 專案嗎?
檢查你的這個配置:

"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/home/test",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
如果你的launchUrl沒有給定默認url或者給定了錯誤的路由,就會出現上面的錯誤,并且找不到默認的啟動路徑。您可以添加您需要的內容,例如:
控制器
namespace WebApplication130.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class HomeController : Controller
{
[Route("test")]
public string Index()
{
return "sucess!";
}
}
}
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/415861.html
標籤:
上一篇:在嵌套查詢中內連接多個表
