Fixing ASP.NET Core Feature Folders Causing Resharper Intellisense Issues

Updated: 2020-05-03.  Thanks to Luke in the comments for his tip about Partial Views.

 

I’ve had a handful of people ask me about this, so I figured I’d just blog on it.

The Problem

If you use Feature Folders in ASP.NET Core you may see something like this where Resharper can’t figure out where your Views are.  It will outline your View() calls in red (as well as your partials).

2017-08-24_22-57-12

 

When you run the app it still works, but it leaves a little to be desired with the Developer experience.  Feature Folders are supported in Resharper and plain ASP.NET, but in ASP.NET Core Resharper has not added support for Feature Folders yet.

 

The Fix

Luckily, there’s a workaround, that I was first notified of by Bill Sorensen on my blog post on Feature Folders linked above.

1. Install the JetBrains.Annotations NuGet package

2. Add the following attributes to the top of your IViewLocationExpander file, above the namespace*


[assembly: AspMvcViewLocationFormat("~/Features/{1}/{0}.cshtml")]
[assembly: AspMvcViewLocationFormat("~/Features/Shared/{0}.cshtml")]
[assembly: AspMvcPartialViewLocationFormat("~/Features/{1}/{0}.cshtml")]
[assembly: AspMvcPartialViewLocationFormat("~/Features/Shared/{0}.cshtml")]

3. Re-open your Controller and everything should work

 

  • Note – if you have more custom paths in your IViewLocationExpander than just those, then you’ll have to add those routes as well.

 

So your final output should look something like this:


using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Mvc.Razor;
// These annotations make Resharper not complain about not finding the Views.
// ASP.NET Core support coming to R# for Feature Folders soon – https://youtrack.jetbrains.com/issue/RSRP-461882
[assembly: AspMvcViewLocationFormat("/Features/{1}/{0}.cshtml")]
[assembly: AspMvcViewLocationFormat("/Features/Shared/{0}.cshtml")]
[assembly: AspMvcPartialViewLocationFormat("~/Features/{1}/{0}.cshtml")]
[assembly: AspMvcPartialViewLocationFormat("~/Features/Shared/{0}.cshtml")]
namespace WebApplication1.Infrastructure.StartupCustomizations
{
public class FeatureFolderLocationExpander : IViewLocationExpander
{
public void PopulateValues(ViewLocationExpanderContext context)
{
// Don't need anything here, but required by the interface
}
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
// The old locations are /Views/{1}/{0}.cshtml and /Views/Shared/{0}.cshtml where {1} is the controller and {0} is the name of the View
// Replace /Views with /Features
return new[]
{
"/Features/{1}/{0}.cshtml",
"/Features/Shared/{0}.cshtml"
};
}
}
}

 

That’s it, now Resharper is happy!

 

2017-08-24_23-11-51

 

Hope this helps.

 

 

5 thoughts on “Fixing ASP.NET Core Feature Folders Causing Resharper Intellisense Issues

  1. You could also simply do the following in the Startup.ConfigureServices method:

    services.Configure(o =>
    {
    o.ViewLocationFormats.Clear();
    o.ViewLocationFormats.Add(“/Features/{1}/{0}.cshtml”);
    o.ViewLocationFormats.Add(“/Features/Shared/{0}.cshtml”);
    o.ViewLocationFormats.Add(“/Features/{0}.cshtml”);
    });

  2. For those who stumble on this: The intellisense can still fail if you are using Partial views. In which case you can just add the same conventions but with AspMvcPartialViewLocationFormat instead of AspMvcViewLocationFormat

Leave a Reply