RouteDate in ASP.NET Core MVC

285 views Asked by At

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?

1

There are 1 answers

0
Serge On

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;