How to get value of id in Url from Razor View in ASP.NET Core
Asked Answered
T

8

19

I have a simple ASP.NET Core web application with a page like such http://localhost:5050/Posts/Create/3.

In my Razor View Views/Posts/Create.cshtml, how would I be able to get the value "3" so that I can create a link like <a href="/Blogs/Details/3">« Back to Blog</a>?

So far, created the link using the following Tag Helper, but I don't know what to put for asp-route-id:

<a asp-controller="Blogs" asp-action="Details" asp-route-id="" class="btn btn-default btn pull-right">&laquo; Back</a>

I am just using the default mvc route:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

I know I can get it from the Model (i.e., Model.Blog.BlogId), but I'm looking to get it from the Url using something like Request.Query["id"] in Views/Post/Create.cshtml.

I tried asp-route-id="@Context.Request.Query["id"].

Tiliaceous answered 4/2, 2017 at 23:19 Comment(0)
M
17

Something like that Context.GetRouteData().Values["id"]; ?

Also http://www.blakepell.com/how-to-get-the-current-route-in-a-view-with-asp-net-5-mvc-6

Mcginty answered 4/2, 2017 at 23:23 Comment(5)
Yeah, that's not for asp.net core. There is no RequestContext in asp.net core.Tiliaceous
First line is asp.net core, pasted from my project, but maybe not usable exactly as you wish. What about the last link? I haven't tried but it is supposed to be MVC 6.Mcginty
Hey, I just tried Context.GetRouteData().Values["id"] and it works. Can you edit your answer? I will mark it as the correct solution.Tiliaceous
You can also use ViewContext.RouteData.Values["id"]Myxomatosis
I'm using Asp.net Core 2.0 and only @MarkG's solution worked for meDugaid
R
13

Currently in ASP.NET Core 2.1 & 3.1 this works:

Context.Request.Query["id"]
Rowles answered 12/9, 2018 at 21:29 Comment(0)
D
7

in ASP.NET Core 3.1 & .NET 5 & .NET 6 this works for me:

@Context.Request.RouteValues["id"]
Deoxyribose answered 14/7, 2020 at 7:25 Comment(0)
L
5

FWIW: I use @ViewContext.RouteData.Values["yourRouteId"]

Lyris answered 18/1, 2019 at 16:58 Comment(0)
H
4

It looks like in Asp.Net Core 2.2 only the statement "@ViewContext.RouteData.Values["id"]" will get the id from a link like this "https://localhost:44356/Administration/AddUser/0". If you have a url like this one "https://localhost:44356/Administration/AddUser/0?status=False" and you want to get the value of "status" you can use "@Context.Request.Query["status"]"

Hedvige answered 31/1, 2019 at 8:20 Comment(0)
R
3

use [FromRoute] to achieve the same .

@page "myaccount/{id}"

@function{
  [FromRoute]
  public string Id {get;set;}
}
Retch answered 11/6, 2020 at 21:51 Comment(0)
N
0

For aspcore 2.1, use: HttpContextAccessor.HttpContext.Request.Query["id"]

Nigro answered 10/10, 2018 at 11:53 Comment(0)
F
0

This what I have used which is simple c# code works with .net core and pure MVC also:

 var reqUrl = Request.HttpContext.Request;
 var urlHost = reqUrl.Host;
 var urlPath = reqUrl.Path;
 var urlQueryString= reqUrl.QueryString;
 if (urlQueryString == null)
 {
 ViewBag.URL= urlHost + urlPath;
 }
 else
 {
 ViewBag.URL= urlHost + urlPath + urlQueryString;
 }
Farsighted answered 6/11, 2019 at 7:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.