1

I have this code in ASP.NET MVC 3 and now, I'm converting it to ASP.NET Core MVC.

In the class file, I'm reading RouteData values as shown here:

  public class Utilities
  {
        public Utilities()
        {
            _urlWord = Convert.ToString(HttpContext.Current.Request.RequestContext.RouteData.Values["url"]);
        }
  }

How to convert this into ASP.NET Core MVC code? How to read the RouteData values in ASP.NET Core MVC?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Unknown Coder
  • 1,510
  • 2
  • 28
  • 56
  • In asp.net core mvc, the routedata value can be obtained by adding the endpoint routing function, which becomes easier. What version of .net core are you using? Different versions have different methods. You can read this article, which may help you: https://aregcode.com/blog/2019/dotnetcore-understanding-aspnet-endpoint-routing/ – Tupac Oct 04 '21 at 05:39

1 Answers1

1

in net core you can do the same using this

var id = HttpContext.Request.RouteValues["id"];

or using all route values

var values = HttpContext.Request.RouteValues
.Select(v => new { Key = v.Key, Value = v.Value }).ToList();

var idValue = values.FirstOrDefault(v=> v.Key=="id");
var id = idValue.Value;
Serge
  • 40,935
  • 4
  • 18
  • 45