2
votes

Microsoft.AspNetCore.Mvc.Internal.ActionContext.GetNormalizedRouteValue() doesn't exist

I am implementing a custom Razor ViewEngine in .net Core 2.2.

MSDN states that the GetNormalizedRouteValue() method exists within Microsoft.AspNetCore.Mvc.Internal for .Net Core 2.2, but it looks like it doesn't exist when I compile or inspect the assembly.

My Code

My code context where I am using use it is context.GetNormalizedRouteValue(AREA_KEY)

public class CustomViewEngine : IViewEngine
{
 public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
        {
            var controllerName = context.GetNormalizedRouteValue(CONTROLLER_KEY);
            var areaName = context.GetNormalizedRouteValue(AREA_KEY);

            var checkedLocations = new List<string>();
            foreach (var location in _options.ViewLocationFormats)
            {
                var view = string.Format(location, viewName, controllerName);
                if (File.Exists(view))
                {
                    return ViewEngineResult.Found("Default", new CustomView(view, _customViewRendering));
                }
                checkedLocations.Add(view);
            }

            return ViewEngineResult.NotFound(viewName, checkedLocations);
        }

Microsoft's API

When inspecting the Microsoft API, only the following method signatures appear to exist.

namespace Microsoft.AspNetCore.Mvc
{

    public class ActionContext
    {

        public ActionContext();

        public ActionContext(ActionContext actionContext);

        public ActionContext(HttpContext httpContext, RouteData routeData, ActionDescriptor actionDescriptor);

        public ActionContext(HttpContext httpContext, RouteData routeData, ActionDescriptor actionDescriptor, ModelStateDictionary modelState);

        public ActionDescriptor ActionDescriptor { get; set; }

        public HttpContext HttpContext { get; set; }

        public ModelStateDictionary ModelState { get; }

        public RouteData RouteData { get; set; }
    }
}

I've checked MSDN's documentation and it indicates it should exist.

Error

'ActionContext' does not contain a definition for 'GetNormalizedRouteValue' and no accessible extension method 'GetNormalizedRouteValue' accepting a first argument of type 'ActionContext' could be found (are you missing a using directive or an assembly reference?)

2

2 Answers

1
votes

The GetNormalizedRouteValue() method is static and is defined on the Microsoft.AspNetCore.Mvc.Internal.NormalizedRouteValue type.

Try this instead:

var controllerName = NormalizedRouteValue.GetNormalizedRouteValue(context, CONTROLLER_KEY);

See MSDN

1
votes

Yo need to do some changes in your code Make sure you are using

Microsoft.AspNetCore.Mvc.Razor

GetNormalizedRouteValue method is now public. So you can call RazorViewEngine.GetNormalizedRouteValue method

Replace following line

 var controllerName = context.GetNormalizedRouteValue(CONTROLLER_KEY);

Your method will be like this

 public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
  {
   var controllerName = RazorViewEngine.GetNormalizedRouteValue(context, CONTROLLER_KEY);
   var areaName = RazorViewEngine.GetNormalizedRouteValue(context, AREA_KEY)             
                //your code
    }

Mircosoft API

namespace Microsoft.AspNetCore.Mvc.Razor
{

    public class RazorViewEngine : IRazorViewEngine, IViewEngine
    {
        public static readonly string ViewExtension;
        protected IMemoryCache ViewLookupCache { get; }
        public static string GetNormalizedRouteValue(ActionContext context, string key);
        public RazorPageResult FindPage(ActionContext context, string pageName);
        public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage);
        public string GetAbsolutePath(string executingFilePath, string pagePath);
        public RazorPageResult GetPage(string executingFilePath, string pagePath);
        public ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage);
    }
}