How to make URL parameters in ASP?

Good afternoon.
A little theory:
The standard itinerary looks like this:
/Users/Add/Alex/120
Controller/Method/Parameter/Parameter

Can the of this bunch, delete "Method" and replacing it by a parameter, but that controller was perceived as a method with a parameter?
In the end we need to
/Users/Alex/120
with the action "Add"
July 9th 19 at 13:30
2 answers
July 9th 19 at 13:32
Solution
For this there are methods by default.

In standard routing, a method (action) default is "Index", but this can be changed in the configuration routing.

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


this is for the whole site in the Startup class. Pay attention to the snippet action = "Index"

You can either specify a separate route for your controller there or via the attributes:
routes.MapRoute(
 name: "Users",
 url: "Users/{action}/{id}",
 defaults: new { action = "Add", id = UrlParameter.Optional }
);

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

using attributes like this:
[Route]
 public ActionResult Add(int id) { ... }

[Route(“{Index}”)]
 public ActionResult Index() { ... }
there is an example? I do not quite understand, remove action, and then what? - Daphne_Cormier51 commented on July 9th 19 at 13:35
: edited response - lysanne.Rempel commented on July 9th 19 at 13:38
: here's a good tutorial in Russian metanit.com/sharp/mvc5/6.1.php - lysanne.Rempel commented on July 9th 19 at 13:41
in your example, the url "/Users/Alex/120" will be parsed as action = "Alex" id = 120 - aron8 commented on July 9th 19 at 13:44
: I gave an example of how to change the default action by default. I don't know how the routers the author actually is obviously not so, as in the standard mapping, otherwise its /Users/Add/Alex/120 also will not work. - lysanne.Rempel commented on July 9th 19 at 13:47
*ranting and not routers of course :) - lysanne.Rempel commented on July 9th 19 at 13:50
Doing so
[RoutePrefix("Master")]
public class MasterController : Controller
{
// GET: Master
[Route("start")]
public ActionResult Index(){}
...}
As a result, get

"Server error in application '/'." - Daphne_Cormier51 commented on July 9th 19 at 13:53
Thank you all sorted out! Default of ranting and need to remove! - Daphne_Cormier51 commented on July 9th 19 at 13:56
: 1) [RoutePrefix("Master")] is optional 2) [Route("{start}")] 3) don't forget to add routes.MapMvcAttributeRoutes(); pen other rotami to enable routing attributes - lysanne.Rempel commented on July 9th 19 at 13:59
July 9th 19 at 13:34
If you are using a WebAPI controller it is possible so:

public static class WebApiConfig
{
 public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
 name: "DefaultApi",
 routeTemplate: "api/{controller}/{id}",
 defaults: new { id = RouteParameter.Optional }
);

}
}

public class UsersController : System.Web.Http.ApiController
{
[HttpPost]
 public void Add(string id, int p1)
{
}
}


but the url should be /Users/Alex?p1=120 and the POST method (see the attribute, and you can GET, but not well-getter, something to add) then id="Alex" p1=120

Find more questions by tags ASP.NET