MVC routes.MapRoute name property

I am new to MVC, so please bear with me, as I am only on the second page of the MS Tutorial (see the last code example). The following MapRoute has been added for HelloWorldController:

routes.MapRoute( name: "Hello", url: "{controller}/{action}/{name}/{id}"); 

I'm just wondering, is this purely matching the pattern that does the job, and the name "Hello" is only for my own link? If so, should you name the conventions that should be followed, saying that MapRoute should be called HelloWorldWelcome, where the method inside HelloWorldController.cs is welcomed (see link above). Or am I pedantic?

+5
source share
4 answers

The route name is also used by the UrlHelper class. For instance:

 var url = Url.Route("Hello", new { controller = "SomeController", action = "SomeAction", name = "charlie", id = 123 }); 

This will result in the creation of an appropriate URL.

This feature is much more useful when using attribute routing. For example, if there is an action on some controller:

 [RoutePrefix("api/phonebook")] public class PhonebookController { [HttpGet("contact/{id}", Name = "GetContact")] public Contact GetContact(int id) { ... } } 

In other code, you can use Url.Route("GetContact", new { id = 7 }) to create the URL /api/phonebook/contact/7 .

+4
source

Read more about MVC MVC Routing Overview

The name attribute is designed to call a font from your views or the controller with the route name.

From ActionLink, you can use program_name:

  Html.RouteLink("link_text", "route_name", route_parameters) 
+1
source

The question doesn’t seem to have answered so clearly (how is the Hello route selected by the HelloWorld controller?), But as an Asp.Net MV5 begginer, I see that the route is selected by default in accordance with the match between the property of the router URL and URL parameters.

0
source
 routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "ImageScan", action = "ScanImage", id = UrlParameter.Optional }, namespaces: new[] { "WebApplication3.Controllers" } ); 

I find an error: Description: HTTP 404. The resource you are looking for (or its dependencies) may have been deleted, its name changed or temporarily unavailable. Review the following URL and make sure it is spelled correctly.

Requested URL: /Views/ImageScan/ScanImage.cshtml

0
source

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


All Articles