當我想在我的 Mac 上使用帶有 Visual Studio 2019 程式的 .Net Core MVC 架構運行我的專案時,出現錯誤“無法找到此本地主機頁面”。我正在共享 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();
});
}
}
}
供應商注冊控制器.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熱心網友回復:
看起來它是 API,但您沒有起始頁。默認情況下,它通常是 Home/index 操作。這就是為什么你有這么奇怪的 screen ,但 API 會正常作業。
我不喜歡這種奇怪的螢屏,通常會添加一個起點。只需將這樣的內容添加到您的控制器中即可。動作的名稱是什么并不重要,最重要的是它應該有一個根路由。
[Route("/")]
public IActionResult Start()
{
var content = "<html><body><h1>Hello!</h1><p> <h3> Test API Service Is Ready To Work!!! </h3> </p></body></html>";
return new ContentResult()
{
Content = content,
ContentType = "text/html",
};
}
我正在渲染視圖,但您可以創建一個真實的視圖并回傳。
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/houduan/328972.html
