Homepages in ASP.NET MVC

I'm trying ASP.NET MVC, but after reading a huge tutorial, I'm a little confused. I understand how controllers have actions that URLs are directed to, but how do homepages work? Is the home page its own controller (for example, "Home"), which has no action? This sounds correct, but how is it realized without action without action (no action means any methods that invoke the viewing mechanism)?

In other words, my question is: how are homepages implemented (in terms of controllers and views)? Could you provide some sample code ?

+3
source share
3 answers

The "main page" is nothing more than arbitrary Actionin the concrete Controller, which returns a certainView

To set the Home page, a page or better articulated on the default page, you need to change the routing information in the file Global.asax.cs:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        AreaRegistration.RegisterAllAreas();

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "NotHome", action = "NotIndex", id = "" }  // Parameter defaults
        );

Pay attention to the definition of the route:

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "NotHome", action = "NotIndex", id = "" }  // Parameter defaults
        );

This route is a "caught" route, that is, it will take any URL and break it into a specific controller, action and identifier. If none or one of the routes is defined, it will use the default values:

new { controller = "NotHome", action = "NotIndex", id = "" }

: " - , , NotIndex NotHome". "", , "Default.aspx", "Index.html" MVC.

+7

/ .

, , , HomeController Index , , :

routes.MapRoute(
    "Default",                                              
    "{controller}/{action}/{id}",                           
    new { controller = "Home", action = "Index", id = "" });
+3

It depends on what you mean by the "homepage". If you mean the page viewed at http://www.yoursite.com (without the page or controller name), then this is an index controller that works like any other, except that you do not see the controller name in the URL address.

0
source

Source: https://habr.com/ru/post/1737392/


All Articles