0

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?

1
  • Just a minor point, and not the answer to your question (Chris Pratt has given that): generally UseStaticFiles() is called before UseMvc(). Sequence also matters with respect to routes. Commented Apr 1, 2019 at 15:12

1 Answer 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...";
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

StackOverflow can be a tough place, especially for newcomers. Don't take it personally, some people just never learned how to be civil and polite. I've up-voted you for what it's worth.
I personally wouldn't have downvoted the question, but upvoting just because someone else downvoted is equally as bad in my opinion. I general I've found very few people actually understand what warrants an upvote or a downvote and use them almost indiscriminately. For a question, an upvote means that it is well research, clear, and useful. A downvote obviously means the opposite: lack of research, unclear, not useful. I could see "lack of research" being a motivation, as this is technically documented. However, it's also easy to miss in the documentation.
@Chris Pratt I think SO is to get the answer quicker saving your time for research and that's the whole point, isn't it? Answer is always hard for new comers for the field but so obviously for experienced one. Can't we ask question after like 5mins of research? Is't better if the system ask for reason when you down vote, and later others can review it, and the down voter lose points for mis-down voting.
@ SRQ Coder Thanks that's so nice of you.
SO is a repository of answers to common questions, so yes, it serves to help you get the answer "quicker" if you're finding those existing answers in your research. However, if your question isn't covered, then it's on you to do the appropriate research, and attempt a solution before coming here to ask. Stack Overflow isn't a replacement for your brain.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.