I have just started learning .NET Core MVC .I am creating a contact us view page .My doubt is should i create a structure where it is like this
1)
-
Views(folder)
-ContactUs(folder)-Index.cshtml
-
Controller(folder)
-ContactUsController.cs
2)
-
Views(folder)
-Home(folder)
-ContactUs.cshtml
-
Controller(folder)
-HomeController.cs
3) This is similar to 1 just name change of cshtml file .Would like having a page as ContactUs.cshtml be better at seo rather than Index.cshtml?
-
Views(folder)
-ContactUs(folder)-ContactUs.cshtml
-
Contorller(folder)
-ContactUsController.cs
2
Answers
The way MVC load views for each ActionMethod in a controller is following this order:
For example, for a controller:
The usual folder structure for this should be:
Now, in this specific case. The usual way to offer a ContactUs page, is making this one an ActionMethod of the HomeController, instead of creating an entire controller for it.
Honestly from a ‘code’ point of view it doesn’t matter. All the ways you’ve outlined above will work fine.
You will have the following URLs from them:
1 –
/contactus
2 –
/home/contactus
3 –
/contactus/contactus
As you can see they will all work but you also need to consider things from an SEO point of view.
Ideally, you’d want a simple URL like
/contactus
to be used so in this case option 1 seems to be the best.That said, you could still use option 2 or 3 but you would need to set up a
Route
in order to make the URL ‘friendly’.Something like this:
For example, in
startup.cs
for Option 2:says point
yoursite.com/contactus
to thehome
controller and thecontactus
action.Note: Custom routes need to be placed before the
default
route.