0

first time i respond on stackexchange like Email form

my question is not about aspx pages, its about asp.net core MVC6 asp.net core can do without aspx pages, it uses Razor pages. Another reason for not removing this question is for helping other students like me who endup with the same question i had based on this course, as i later found the proper words, there are various websites where people seam confused about this, and while i adept to this new concept. I hope you can keep the question more people following the Microsoft online course can stumble into this, as on the video its not realy clear. (lots of things are discussed and this is quite a big one between all the other explanations in that movie).

At some point in this course video, they have defined a Controller that responds to an URL. MVC somehow understands when you request http://localhost/Scott, that it should call the ScottController which then returns "hey it's scott".

The controllers look like this:

namespace WebApplication1.Controllers  // dont use controller minus S (controller) here 
{
    public class MariaController
    {
        public string Index() => "Hello from index mvc";
        public string Maria() => "Hello from Maria mvc";
    }

    public class ScottController
    {
        public string index()
        {
            return "hey it's scott";
        }
    } 
}

Notice in the code above there is no [HttpGet("/Maria")] or [HttpGet("/Scott")] annotation, yet somehow this code knows about that the class ScottController should respond to the /Scott URL, despite it doesn't have a "name property" or so with the value "Scott" to check against.

Is it that with Controller classes, the name "ScottController" is split up and Controller is removed, so MVc knows this a class that handles requests to the /Scott URI?

Peter
  • 2,043
  • 1
  • 21
  • 45
  • 4
    It is the MVC convention. – Shyju Nov 01 '16 at 15:11
  • 1
    It uses Convention over Configuration. Have a look here: http://www.danylkoweb.com/Blog/aspnet-mvc-convention-over-configuration-BU – Steve Nov 01 '16 at 15:11
  • 1
    Possible duplicate of [Convention over configuration in ASP.NET MVC](http://stackoverflow.com/questions/1072477/convention-over-configuration-in-asp-net-mvc) – Clint Nov 01 '16 at 15:24
  • @Clint that one is about views. I could find plenty of (better) duplicates about view conventions, none about controllers. – CodeCaster Nov 01 '16 at 15:25
  • @DarrenYoung thanks for pointing me the to article convention over configuration i bookmarked it now – Peter Nov 01 '16 at 15:53

1 Answers1

1

It's entirely the other way around, and all has to do with routing. The site-relative URI /Scott defaults to these route values:

  • Controller: Scott
  • Action: Index

This is because of conventions. The default convention is /{controller}/{action}, where the action is optional and defaults to Index.

So when you request /Scott, MVC's routing will go and look for a controller named ScottController, all because of conventions. See also Why do MVC controllers have to have the trailing 'Controller' convention on their class name?

Then, because of the action being absent from the request-URI, it will try and find the default action named Index, which returns "hey it's scott".

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • but where does it know that Scott refers to ScottController ?. is Controller behaving like a word that is added to what is typed at the url ?. so http://localhost/scott becomes scot+controller = scottcontroller – Peter Nov 01 '16 at 15:28
  • Yes, that's the convention. See edit. – CodeCaster Nov 01 '16 at 15:31
  • amazing never seen something like that in any other coding language. (and i know quite a lot of languages) been looking at the movie 4 times or so, to see if i was missing something, but i couldnt find it, and also couldn't believe breakingup verbs of a coding language is the answer... it feels very strange to me, feels like allowing the break of a physics rule. – Peter Nov 01 '16 at 15:37
  • It's not "verbs" that you're talking about. In the context of HTTP, a "verb" is a request-method, such as GET or POST. Also, MVC relies heavily on reflection, which allows you to inspect types at runtime using strings. Reflection is used in many programming frameworks. Once you can find and instantiate types with strings, any kind of magic like this becomes trivial. – CodeCaster Nov 01 '16 at 15:39
  • not seen it in 14 languages i used before, but its a strange situation. Its a class nameSpecial, instead of a class Type causing a behavior. Well i just will have to get used to it. – Peter Nov 01 '16 at 15:52
  • I have no idea what you mean by that. Reflection is not rare. See [this Wikipedia lemma on reflection](https://en.wikipedia.org/wiki/Reflection_(computer_programming)). – CodeCaster Nov 01 '16 at 15:54
  • What happens when I've two ScottController classes (in different namespaces)? Can we specify which namespace to use? – Developer Webs Dec 07 '20 at 21:31