我有一个ASP.NET Core空白项目,它非常适合通过https://localhost/filename提供静态文件。现在,我想向其中添加MVC函数。但是引用https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-controller?view=aspnetcore-2.2&tabs=visual-studio,添加“ Controllers”文件夹后,添加一个控制器类:
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/Welcome/
public string Welcome()
{
return "This is the Welcome action method...";
}
}
StartUp.cs就像:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
app.UseStaticFiles();
}
Builder就像:
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
毕竟,我仍然无法访问“ https://localhost/HelloWorld/Welcome/”。
我省略了什么?
答案 0 :(得分:2)
您没有指定默认路由,也没有为此指定任何形式的路由。最快的解决方法是将app.UseMvc()
更改为app.UseMvcWithDefaultRoute()
。或者,您可以添加属性路由:
[Route("[controller]")]
public class HelloWorldController : Controller
{
[HttpGet("Welcome")]
public string Welcome()
{
return "This is the Welcome action method...";
}
}