7

I've started trying out new ASP.NET Core 2 for building an MVC Web App, and it looks good so far...

Except that when I create Views from my controller actions, ReSharper creates them in a Pages folder, rather than the Views folder (where I generally like to keep my Views ;-) )

resharper create razor view view in pages folder

ReSharper doesn't behave this way for regular ASP.NET Web Apps (not core), it puts the views in the correct view folder, so this looks to be to do with Core / Core 2.

What is resharper using to decide where to create the view?

How can I change this behavior so that it creates the views in the traditional location?

2 Answers 2

11

I think this is a bug in Resharper and I'm suspecting it might be related to the new Razor Pages Apps .net core 2 team just released.

So, perhaps it would be better for you to fill an issue on Reshaper's Bug Tracking System.

However, I've managed to setup a workaround. I'm sharing the steps here, just in case you need to fix this ASAP and when JetBrains fixes the issue - if it finally is an issue :) - you can remove this patch.

  1. Install the nuget package for Jetbrains Annotations.

    Install-Package JetBrains.Annotations
    
  2. Create a .cs file on the root of your asp.net core project - I've named it ResharperConfig.cs - with the following content:

    using JetBrains.Annotations;
    
    [assembly: AspMvcMasterLocationFormat("~/Views/{1}/{0}.cshtml")]
    [assembly: AspMvcViewLocationFormat("~/Views/{1}/{0}.cshtml")]
    [assembly: AspMvcPartialViewLocationFormat("~/Views/Shared/{0}.cshtml")]
    [assembly: AspMvcAreaMasterLocationFormat("~/Areas/{2}/Views/{1}/{0}.cshtml")]
    [assembly: AspMvcAreaViewLocationFormat("~/Areas/{2}/Views/{1}/{0}.cshtml")]
    [assembly: AspMvcAreaPartialViewLocationFormat("~/Areas/{2}/Views/Shared/{0}.cshtml")]
    
  3. Build your project and restart VS. (this was important for me).

  4. When you reopen the solution, R# will recognize the locations for the views. Now you can go and create views from the actions the way you're used to.

Resharper issue

Hope this helps!

8
  • So... this is a pretty epic answer, and clearly well researched and probably works, and I'll mark it as accepted. However, I updated R# before seeing your answer, and it now works correctly! Thank you anyway - it's taught me about how to customize the R# behavior.
    – Erresen
    Commented Sep 28, 2017 at 18:34
  • 2
    We could go crazy speculating here :) - maybe after the update R# overwrote the config values with the correct ones for instance - but I don't see much benefit in doing that. I'm glad it worked for you anyways. Commented Sep 28, 2017 at 19:22
  • 1
    Thanks for this answer. :)
    – Zack
    Commented Jan 9, 2018 at 17:30
  • 1
    See resharper issue RSRP-465732, planned to fix in 2018.1
    – A K
    Commented Mar 12, 2018 at 13:47
  • 1
    Thank gawd been driving me mad for ages (years) this. I commented on that issue on RSRP in 22 Oct 2018 and it still isn;t fixed for old MVC asp.net projects! Thanks
    – Rippo
    Commented Mar 30, 2020 at 11:52
1

This still applies to the ASP.NET Core Web App (Model-View-Controller) .NET 6 template installed with VS 2022. R# generates views in \Pages by default, which is the wrong location anyway.

With the ResharperConfig.cs (as described above) now R# creates the views in the right place, assuming the View's parent folder exists.

My entity centered Clean Architecture configuration:

using JetBrains.Annotations;

[assembly: AspMvcMasterLocationFormat("~/Views/{1}/{0}.cshtml")]
[assembly: AspMvcViewLocationFormat("~/{1}/Views/{0}.cshtml")]
[assembly: AspMvcPartialViewLocationFormat("~/Views/Shared/{0}.cshtml")]
[assembly: AspMvcAreaMasterLocationFormat("~/Areas/{2}/Views/{1}/{0}.cshtml")]
[assembly: AspMvcAreaViewLocationFormat("~/Areas/{2}/Views/{1}/{0}.cshtml")]
[assembly: AspMvcAreaPartialViewLocationFormat("~/Areas/{2}/Views/Shared/{0}.cshtml")]

This [assembly: AspMvcViewLocationFormat("~/{1}/Views/{0}.cshtml")] makes sure R# generates views in this location: \<Controller>\Views\<Action>.cshtml

You should combine that with a IViewLocationExpander to instruct .NET to also look in the same location to find the views while rendering them:

using Microsoft.AspNetCore.Mvc.Razor;

public class CleanArchViewLocationExpander : IViewLocationExpander
{
    public void PopulateValues(ViewLocationExpanderContext context)
    {
    }

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        return viewLocations.Select(l => l.Replace("/Views/{1}/", "/{1}/Views/"));
    }
}

Which you then register in Program.cs like this:

builder.Services.Configure<RazorViewEngineOptions>(
    options =>
    {
        options.ViewLocationExpanders.Add(new CleanArchViewLocationExpander());
    });

Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.