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
The way MVC load views for each ActionMethod in a controller is following this order:
/Views/[Controller]/[ActionMethod].cshtml
/Views/Shared/[ActionMethod].cshtml
/Pages/Shared/[ActionMethod].cshtml
For example, for a controller :
public IActionResult View1()
{
return View();
}
public IActionResult View2()
{
return View();
}
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:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "contact",
defaults: new { controller = "Home", action = "ContactUs" });
template: "contactus");
});
says point yoursite.com/contactus
to the home
controller and the contactus
action.
Note: Custom routes need to be placed before the default
route.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.