MarkCo
MarkCo

Reputation: 1212

Changing the default controller on start of program

I created a new controller and view in my asp.net 5.0 mvc project and wanted that new controller to be the controller that is viewed on the very first start of the program instead of the default Home controller. The issue I am facing is that when I start the application it tells me

invalidOperationException: The view 'UserIndex' was not found. The following locations were searched: /Views/UsernameEntry/UserIndex.cshtml /Views/Shared/UserIndex.cshtml

UsernameEntryController:

public class UsernameEntryController : Controller
{
    // GET: UsernameEntryController
    public ActionResult UserIndex()
    {
        return View();
    }

}

I then have a folder inside of views named UsernameEntry and the file name is UserIndex.cshtml

which contains <h2>User entry testing</h2>

I saw online that if I wanted to change the default controller on the start of the program I would need to make a modification in the Startup.cs file under the Configure method from this:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });

to

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=UsernameEntry}/{action=UserIndex}/{id?}");
        });

but it did not work I even went to the properties of that UserIndex.cshtml and changed the build action from Content to Embedded resource but that still gave me the same message from above.

Upvotes: 0

Views: 767

Answers (1)

mm8
mm8

Reputation: 169200

This setup works just fine for me:

enter image description here

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=UsernameEntry}/{action=UserIndex}/{id?}");
    });
}

The Build Action of the view is set to its default value of Content.

Upvotes: 1

Related Questions