0

I'm using Visual Studio 2017 and .NET Core 2.2.

Do the following:

  1. New Project and select ASP.NET Core Web Application (press OK)
  2. This takes you to a second dialog where you can select Web Application (has example Razor Pages) or Web Application (Model-View-Controller).

I want to point out one difference between the two options, when looking at Startup.cs:

If you select just Web Application, you will get this at the bottom of the Configure function:

        app.UseMvc();

If you select Web Application (Model-View-Controller), you will get this:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

So, the MVC project defines some type of routing. Which specifically tells me that it is going to use the Home Controller by default and if no action is specified then the Index(.cshtml) action will be used.

The first option (Razor pages), routes nothing.

I don't understand. Does this mean that if you don't define a MapRoute it will just always use the Index(.cshtml) action?

Because With the MVC Project, if I used just:

        app.UseMvc();

and defined no MapRoute, I would get a 404. How is the Razor app defining it's routing (and where)?

JustLooking
  • 2,405
  • 4
  • 28
  • 38
  • None of the options have absolutely nothing to do with `Index.cshtml`, and this "it is going to use the Home Controller with it's associated Index.cshtml file" is **not** true. That just tells it's going to use the HomeController *by default* and if no action is specified then the Index action would be used – Camilo Terevinto Mar 20 '19 at 23:59
  • "None of the options have absolutely nothing" is a double negative. :) ... I kid. Anyhow, please forgive me as it might be obvious that I am very new to this and was clumsy with my language. – JustLooking Mar 21 '19 at 00:10
  • Yeah, blame non-native English and tiredness :) For Razor Pages (first option) look [here](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/razor-pages-conventions) and for MVC (second option) [here](https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing) – Camilo Terevinto Mar 21 '19 at 00:15
  • First link was broken, but the second option ... I was there already (before I posted). It talks of setting up routes.MapRoute, just like the MVC project, but nowhere in that article does it describe why it goes directly to index.cshtml if you define no route. I'm trying to scratch that itch! – JustLooking Mar 21 '19 at 00:18
  • Ah sorry, I removed the last s from the url, fixed! And it does answer that, look right under "The route template:" with 3 bullet points at the top of the doc. And as for the View, in another doc in that tutorial series explains that using `return View()` returns a View with the name of the Action (so Home/Index => Views/Home/Index.cshtml) – Camilo Terevinto Mar 21 '19 at 00:20
  • Eeek! Im going to have to disagree on that second link. It says Using the default route: routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); The route template: (3 bullet points). ... See, that's actual routes.MapRoute code, just like the MVC project. But the project I created (razor) does not have any code in it that contains the word "MapRoute". So my question was, in the absence of this code, where does it define that this is the default route. – JustLooking Mar 21 '19 at 00:25
  • The first link explains it. I'm not used to Razor Pages (I hate the concept behind it) so I cannot describe it briefly as I can with MVC – Camilo Terevinto Mar 21 '19 at 00:27
  • Hrmm, at a glance, still not seeing it (first link). But I'm also 45 minutes later at work then I wanted to be! Wife is texting, so I'll give it a slower read tomorrow. Thanks for your time. – JustLooking Mar 21 '19 at 00:31
  • Yeah, re-reading today. Neither of those links answer or even talk about what I asked. I guess in the absence of some type of custom or additional routing, it just magically looks for index.cshtml. I'll hold out hope someone can chime in to confirm! – JustLooking Mar 21 '19 at 16:16
  • Just found this SO post: https://stackoverflow.com/questions/48684461/razor-pages-default-page-in-aspnetcore-2?rq=1 ... where it was said in the question: "By default a Razor Page app goes to Home/Index" and in the accepted answer it says: "The docs say the runtime controls the search for Index as the default". I was just hoping to find some actual text/code/file that I could open and go "aha! that's where it's written!" – JustLooking Mar 21 '19 at 16:20

0 Answers0