How to retrieve address ID?

0

I'm using asp.mvc. In my route has the ID, the address of the page for example has link .

I wanted to get this number 1 at the bottom of the page, is there any way to do that?

    
asked by anonymous 08.07.2018 / 04:50

2 answers

0

The default route looks something like this:

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

Note that a id is passed at the end of it, so when you have a route, example: link means that you can get this value by creating a id variable in action

public ActionResult SuaAction(int? id) {}

Now if you want to pass as link , you would need to create a custom route that would look like this:

routes.MapRoute(
    name: "RotaCustom",
    url: "SuaURL/{id}",
    defaults: new { controller = "Controller", action = "Action" },
    constraints: new { id = @"\d+" }
);
    
25.07.2018 / 02:56
-1

You can retrieve the last segment, for example:

// http://usuario/1
string url = Request.Url.AbsoluteUri;
string id = url.Substring(url.LastIndexOf('/') + 1);
//resultado: 1
    
25.07.2018 / 03:13