0

我有一个 ASP.NET Core 空白项目,通过https://localhost/filename提供静态文件非常有用。现在我想给它添加 MVC 函数。但是在添加“Controllers”文件夹后,参考https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-controller?view=aspnetcore-2.2&tabs=visual-studio,添加一个控制器类:

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();
    }

生成器就像:

return WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

毕竟,我仍然无法访问“ https://localhost/HelloWorld/Welcome/ ”。

我遗漏了什么?

4

1 回答 1

2

You have no default route specified, or routing of any sort for that matter. The quickest fix is to change app.UseMvc() to app.UseMvcWithDefaultRoute(). Alternatively, you can add attribute routes:

[Route("[controller]")]
public class HelloWorldController : Controller
{

    [HttpGet("Welcome")]
    public string Welcome()
    {
        return "This is the Welcome action method...";
    }
}
于 2019-04-01T13:01:05.893 回答