How to recover address ID?

Asked

Viewed 100 times

0

I’m using Asp.mvc. On my route has the ID, the page address for example has http://user/1 .

I wanted to get this number 1 at the bottom of the page, there’s some way to do it?

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 at the end of it, this indicates that when you have a route, example: http://seusite/Controller/Action/value you mean it’s possible to catch this value creating a variable id in action

public ActionResult SuaAction(int? id) {}

Now if you wish to pass as follows http://seusite/Controller/value, it would be necessary 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+" }
);

-1

You can recover last segment, for example:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.