ReSharper and custom ASP.NET MVC view location

Since ReSharper 5, we’ve had splendid support for ASP.NET MVC. One of the features included for ASP.NET MVC is Navigate to View and Create View. When working in an ASP.NET MVC action method, these methods allows us to either navigate to the corresponding view or create a view with the correct name at the correct location.

The interesting part of this feature is that ReSharper checks for assignments to the different properties on a ViewEngine, such as ViewLocationFormats and PartialViewLocationFormats, to determine the paths to various view types. This means it is possible to diverge from the default conventions in ASP.NET MVC and still make use of the ASP.NET MVC support ReSharper gives us.

Imagine having a project in which views are not located in the conventional Views folder but instead in another subfolder, like UI/MVC/Razor. We can do this in ASP.NET MVC by creating a new ViewEngine implementation and specifying a path template in the constructor:

To ensure ASP.NET MVC makes use of this custom ViewEngine, we must register it in Global.asax under Application_Start:

What’s interesting is that ReSharper immediately tells us no views can be found:

We can now move our views to the correct location or have ReSharper create them for us using the Create View feature. The new view will be located in our project under UI/MVC/Razor/Home/Index.cshtml.

Pretty slick if you ask me!

This entry was posted in How-To's, ReSharper Tips&Tricks and tagged , , , . Bookmark the permalink.

14 Responses to ReSharper and custom ASP.NET MVC view location

  1. Robert Slaney says:

    Would be nice if you could explicitly tell R# what the new convention is. If the view engine configuration is NOT in the exact place the web project it doesn’t work.

    We use a ViewEngine class definition from another assembly and R# can’t discover it.

  2. Maarten.Balliauw says:

    @Robert R# uses assignments to ViewLocationFormats as the method of determining where views are located. Can you try doing this assignment in the web project?

  3. Kiliman says:

    I’ve got a method named RenderPartialViewToString(string viewName, object model).

    Is there a way to decorate that method so Resharper knows that it should look for views name viewName?

    That would be awesome.

  4. Maarten.Balliauw says:

    @Kiliman yes there is. Checkout http://www.jetbrains.com/resharper/webhelp/Reference__Code_Annotation_Attributes.html on some attributes that are supported by ReSharper and allow you to do just that.

  5. David says:

    Does the same thing go for controllers. I am thinking of using the Single Action Controller idea and I am curious if there is some way to make sure resharper doesnt freak out.

  6. lifetime of e cig battery says:

    Asp.net tool use 2.0 framework.In asp.net we are create a project then three language Vb,C# and R# file..C# is same as a VB.net in Asp.net
    thanks give the info R#(resharper )

  7. Zache says:

    Is it possible for this feature to work using a ViewEnginge similiar to this?

    http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx

  8. Maarten.Balliauw says:

    That will indeed work.

  9. Kijana Woodard says:

    “Zache Says:
    April 19th, 2013 at 3:45 pm
    Is it possible for this feature to work using a ViewEnginge similiar to this?

    http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx

    Maarten.Balliauw Says:
    April 19th, 2013 at 5:54 pm
    That will indeed work.

    I tried it. It doesn’t appear to work. R# 7 creates a folder at “/%1/{ControllerName}/{ViewName}.cshtml”

  10. The %1 will indeed not work as R# is unable to figure that one out. If this is a requirement please file an issue at http://youtrack.jetbrains.com/issues/RSRP

  11. Kijana Woodard says:

    Done: http://youtrack.jetbrains.com/issue/RSRP-386656

    It’s a little different than the %1 case.
    Hopefully a little easier. :-]

  12. Jeff says:

    Is there a way to get this to work without clearing out the default view locations? The example I found online for a custom view engine did a union to merge the new custom format with the existing view locations. But when I do this resharper doesn’t recognize my new location. If I follow the example above and only set my new location then Resharper works for resolving views in the new location but Resharper (and .net) cannot find views still in the original locations.

    This is what I have in my custom view engine…

    private static readonly string[] NewViewFormats = new[]
    {“~/Areas/Customer/Views/Security/{1}/{0}.cshtml”};

    public CustomerAreaViewEngine()
    {
    base.ViewLocationFormats = base.ViewLocationFormats.Union(NewViewFormats).ToArray();
    base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewViewFormats).ToArray();
    }
    }

  13. Unfortunately it will currently not work with the Union() in there. If you assign it a full array, this will work.

  14. Jeff says:

    Ok, I guess I’ll just have to copy the format strings from the base RazorViewEngine to use in my custom engine then. I was hoping to not have to copy and paste to re-use the default values but I couldn’t get it to recognize the new location anytime that I tried to combine it with the locations that were already added by the base view engine.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>