jw_
jw_

Reputation: 1785

How to bring in ASP.NET Core MVC to an existing ASP.NET blank project?

I have an ASP.NET Core blank project, and it works great to serve static files through https://localhost/filename. Now I want to add MVC functions to it. But referencing https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-controller?view=aspnetcore-2.2&tabs=visual-studio , after adding "Controllers" folder, add a controller class:

public class HelloWorldController : Controller
{
    // 
    // GET: /HelloWorld/Welcome/ 

    public string Welcome()
    {
        return "This is the Welcome action method...";
    }
}

StartUp.cs is like:

    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 is like:

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

after all this, I still can't access "https://localhost/HelloWorld/Welcome/".

What did I omit?

Upvotes: 0

Views: 135

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239290

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...";
    }
}

Upvotes: 2

Related Questions