20 Replies Last post: Mar 13, 2014 4:29 AM by Maksim Vi  
Maksim Vi Newbie 12 posts since
Feb 19, 2014
Currently Being Moderated

Feb 19, 2014 1:38 AM

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.

Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Feb 20, 2014 1:26 PM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder

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

Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Feb 20, 2014 2:32 PM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder

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):

 

  • AspMvcAreaMasterLocationFormatAttribute
  • AspMvcAreaPartialViewLocationFormatAttribute
  • AspMvcAreaViewLocationFormatAttribute
  • AspMvcMasterLocationFormatAttribute
  • AspMvcPartialViewLocationFormatAttribute
  • AspMvcViewLocationFormatAttribute

 

You can add more than one of each type, and you'd use them like this:

 

[assembly: AspMvcViewLocationFormat("~\Admin\Views\{1}\{0}.aspx")]
[assembly: AspMvcViewLocationFormat("~\Admin\Views\{1}\{0}.ascx")]
[assembly: AspMvcViewLocationFormat("~\Admin\Views\{1}\{0}.cshtml")]
[assembly: AspMvcViewLocationFormat("~\Admin\Views\Shared\{1}\{0}.aspx")]
[assembly: AspMvcViewLocationFormat("~\Admin\Views\Shared\{1}\{0}.ascx")]
[assembly: AspMvcViewLocationFormat("~\Admin\Views\Shared\{1}\{0}.cshtml")]

 

And so on, for views, partial views, masters and for when you're in an area. The replacement values are:

 

  • {0} - view name
  • {1} - controll name
  • {2} - area name

 

ReSharper will use these path format strings to look for the different views.

Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Feb 21, 2014 1:45 PM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder

Try backslashes. I'm not sure if it requires them or not...

Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Feb 24, 2014 1:46 PM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder

Would you be able to share some code? You can email me directly at matt.ellis@jetbrains.com

Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Feb 27, 2014 10:23 PM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder

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.

Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Feb 28, 2014 3:04 AM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder

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?

Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Feb 28, 2014 12:57 PM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder
Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Mar 3, 2014 1:40 PM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder

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.

Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Mar 6, 2014 2:09 PM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder

Navigation from View to Controller only works if you reference an action in the view, e.g. with something like:

 

@Html.Action("RemoveAccountList")

 

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?

Matt Ellis Novice 364 posts since
Jul 5, 2009
Currently Being Moderated
Mar 11, 2014 4:32 PM in response to: Maksim Vi
Re: ASP.NET MVC in a subfolder

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?

More Like This

  • Retrieving data ...