ASP.NET MVC in a subfolder
I have an external library with the following folder structure:
DB
Admin
Areas
Controllers
Models
Views
Web.config
Logic
Etc
The problem I have is that resharper doesn't highlight my views in controller actions and suggests to create a new view in Root -> Views folder instead of Admin -> Views
Is there a way to make resharper work with a current subfolder instead of placing everything in root folder?
I'm using MVC 5.1.
Please sign in to leave a comment.
Unfortunately, MVC5.1 doesn't get recognised by ReSharper 8.1. We ship support for MVC in the form of external annotations - xml files that ReSharper uses to apply our annotation attributes to compiled code. 8.1 doesn't ship with annotations for 5.1.
You can read more (along with a workaround - we're going to start shipping external annotations as a normal ReSharper extension, enabling out of band releases) here: http://stackoverflow.com/questions/21488176/resharper-navigate-to-mvc-view
Hope this helps
Matt
In that case, you can add your own annotations to tell ReSharper where you've placed your views. Firstly, you need to include the annotations in your project. The easiest way to do this is to go to ReSharper -> Options -> Code Annotations -> Copy default implementation to clipboard, add a new file to your project and paste the implementation in.
Then, you need to add some of these assembly level attributes (usually in your AssemblyInfo.cs, but it can be anywhere):
You can add more than one of each type, and you'd use them like this:
And so on, for views, partial views, masters and for when you're in an area. The replacement values are:
ReSharper will use these path format strings to look for the different views.
Thanks, Matt. I added the following mvc view location formats to my AssemblyInfo.cs, but resharper is still not able to resolve my views:
[assembly: AspMvcMasterLocationFormat("~/Admin/Views/{1}/{0}.cshtml")]
[assembly: AspMvcViewLocationFormat("~/Admin/Views/{1}/{0}.cshtml")]
[assembly: AspMvcPartialViewLocationFormat("~/Admin/Views/Shared/{0}.cshtml")]
[assembly: AspMvcAreaMasterLocationFormat("~/Admin/Areas/{2}/Views/{1}/{0}.cshtml")]
[assembly: AspMvcAreaViewLocationFormat("~/Admin/Areas/{2}/Views/{1}/{0}.cshtml")]
[assembly: AspMvcAreaPartialViewLocationFormat("~/Admin/Areas/{2}/Views/Shared/{0}.cshtml")]
Try backslashes. I'm not sure if it requires them or not...
I just coppied those from my ViewEngine, but backslashes don't work either.
Would you be able to share some code? You can email me directly at matt.ellis@jetbrains.com
Thanks again for looking into the issue.
I updated a sample github I used to show the issue in a ticket I created earlier - https://github.com/Untit1ed/ResharperIssue6730
In that project take a look at MVC folder located in root directory. Its controller can't resolve its own views - http://i.imgur.com/AqjiIH6.png
But as soon as I place MVC in Areas folder it works as expected - http://i.imgur.com/DU5YbG5.png
Unfortunately, it looks like you've uncovered a bug. These annotations only work if they exist in referenced (and therefore compiled) assemblies, or are defined in external annotations. You can't define them in source, not even in a referenced project. I've reported it as an issue: http://youtrack.jetbrains.com/issue/RSRP-406631, please feel free to vote up. One workaround would be to define these annotations in a compiled assembly, reference that assembly, but don't use any members of it - ReSharper should pick it up, but your application won't need to deploy it, because the project doesn't use any of the code in it.
Yeah, there's already a ticket oppened for me on youtrack - http://youtrack.jetbrains.com/issue/RSRP-406166
I'm not sure if I understand the workaround correctly though. In my actual project resharper annotations are in a separate assembly already that is referenced in my MVC library, but it doesn't seem to solve the issue.
Cool, I've linked the issues so that your ticket now has some context on the cause of the issue.
The workaround is this - create a new class library project, put your assembly level annotations in it and compile it. Take that binary, and add a reference to it from your original project. ReSharper will look at all compiled references when looking for MVC attributes, so it should pick up your annotations. Because you're not actually using any code in this reference (only ReSharper is), you haven't added a deploy-time dependency, so you don't need to deploy this dummy assembly any where. Does this make more sense?
It does, I just have to restart Visual Studio every time I modify that external assembly so ReSharper can pick up changes. Thanks.
UPD: this solution actually works much better for me, since I have multiple external libraries containing web views that use the same view location format.
Logged! http://youtrack.jetbrains.com/issue/RSRP-406716
Another issue, but somewhat related. Now in my views I can't navigate back to view's controller (context menu used to have an option "Go To Controller"). Is there something similar to ViewLocationFormat strings, but for controllers?
Can you navigate from the controller to the view? I think the reverse navigation follows the same reference, so if the annotation isn't working, the reference isn't there and there's no navigation.
Yeah Controller -> View navigation works with external assembly annotations, well.. kind of... areas locations are still broken (I updated the github project to demonstrate it https://github.com/Untit1ed/ResharperIssue6730/tree/External-Annotations).
But View -> Controller navigation doesn't work at all.
Navigation from View to Controller only works if you reference an action in the view, e.g. with something like:
Do you views have something like this in them, and do they have underlines to navigate to the controller (on the 'Action' method name) and the action method (on the 'RemoveAccountList' string literal)?
As for areas, as I understand it, area controllers need to live under the 'Areas' folder - if you change your project to move the controllers to ~\Areas\MVCArea\Controllers\ then it starts working, navigating to the correct view in ~\MVC\Areas\MVCArea\Views\<Controller>\. Is it possible to have Controllers living in an area in another location in MVC?
My bad, navigation with references works just fine, It's just I'm missing an option in a context menu to navigate back to the controller.
As for areas:
My views are located in "~\MVC\Areas\<Area>\Views\<Controller>\<View>" and controllers are in "~\MVC\Areas\<Area>\Controllers\<Controller>" (default mvc locations + MVC subfolder), but Views still couldn't be resolved in Controllers' actions with the correct external r# annotations:
"~\Admin\Areas\{2}\Views\{1}\{0}.cshtml"
The dev in charge of the MVC annotations has just reminded me that ReSharper parses the source of the current project and looks for any of these format location strings being assigned to the PartialViewLocationFormats or ViewLocationFormats properties of the view engines. Are you setting these at all?
Yes, they are all set.
So I shouldn't expect this to be fixed at all?