How to get the url for different action methods in one controller without the controller name in the url?

I am working on a simple MVC website. In my project there is only one controller ( Home) and 2 methods of action in it ( Indexand Innerpages). Homethe action returns a view for the home page, and the Innerpage action returns content for the inner pages (inner pages use a single template, so use a single view for all inner pages).

Now all I want, if I run the project, I got a menu like:

http://localhost:3000/Home/
http://localhost:3000/Home/Info/AboutUs
http://localhost:3000/Home/Info/Contact

But instead of the above path, I need such paths as:

http://localhost:3000/Home/
http://localhost:3000/AboutUs
http://localhost:3000/Contact

without adding a new controller, and the URL should invoke the appropriate action methods.

My routing file

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

Could you help me with this?

+4
1

. , . , "HomeRoute", ...

routes.MapRoute(
            name: "HomeRoute",
            url: "{action}",
            defaults: new { controller = "Home", action = "Index" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

, , "", ,

, :

public class HomeController : Controller
{
    public ActionResult Index(){...}
    public ActionResult About(){...}
}

About ...

_/

+4

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


All Articles