1

So, I am new in asp.net core, and I am trying to create a new web api in asp.net core. The default controller is Weatherforecast, in other words when I debug the application in the browser the main page is always Weatherforecast. I added a new controller named MainController.cs, and I wrote some simple code there:

[Route("[controller]")]
[ApiController]
public class MainController : ControllerBase
{
    [HttpGet]
    public string getmain()
    {
        return "welcome";
    }

    
}

} in a word, i want my application's main controller to be https://localhost:43372/main and not https://localhost:43372/weatherforecast, also I tried to delete Weatherforecast controller and Weatherforecast.cs file, however when I deleted it, the main page was still https://localhost:43372/weatherforecast, but with error because I deleted the files. So, how can i make my new controller default for me web app?

3 Answers 3

3

You have to modify launchUrl in the launchSettings.json file that exists in the properties folder.

"launchUrl": "weatherforecast" -> "launchUrl": "main"

Please make sure that the main is a valid controller.

Sign up to request clarification or add additional context in comments.

1 Comment

@givexa Once any response solves your problem, please accept it as answer so that it adds to the reputation of the person who helped you.
1

I suggest you take a little look at Properties/launchSettings.json.

If you want to create a new controller, Controller / Right Click / Add / Controller.

2 Comments

oh thanks it worked! I just had to change launchurl's from weatherforecast to main in Properties/launchSettings.json....
You're welcome, you can contact me when you have problems :)
1
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Main}/{action=getmain}/{id?}");
 });

2 Comments

Code-only answers are not the most helpful, even though they might answer the question. Please explain in a few lines how this answers the question.
Upvoted as this is a better fix. Updating the launch settings is ok, but the endpoint in the Startup.cs file ill still be pointing to WeatherForecast. code only answers aren't too helpful, but the question was quite straight forward. IMO

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.