R# 7 ASP.NET MVC Razor cannot find partial view inspection issue

We change our partial view search format for razor to use an underscore infront of the filename.

R# marks our PartialView methods as errors as it doesn't use the same filename conventions.  How can I change ( or define ) the convention that R# uses ( Option, Template, Attribute, etc ) ?

5 comments
Comment actions Permalink

We change our partial view search format for razor to use an underscore infront of the filename.


Robert, please, provide your code for changing view search format.

0
Comment actions Permalink

I patch the existing locations in a subclass of the razor view engine.

So the partial formats ( as currently defined in MVC3 ) become


PartialViewLocationFormats = { "~/Views/{1}/_{0}.cshtml", "~/Views/Shared/_{0}.cshtml" }
AreaPartialViewLocationFormats = { "~/Areas/{2}/Views/{1}/_{0}.cshtml", "~/Areas{2}/Views/Shared/_{0}.cshtml" }

I also play with the Editor and Display template folders to avoid the default search location becoming the following format "~/Views/{1}/_DisplayTemplates/{0}.cshtml", I correct that to be "~/Views/{1}/DisplayTemplates/_{0}.cshtml" by overriding the FindPartialView method.


I take it that the R# system is trying to discover the location formats by code inspection.  Will that also work is the ViewEngines are declared in a reference assembly and not directly in the MVC web project ?  If so, that is surely an extremely fragile approach.


    public class MyRazorViewEngine : System.Web.Mvc.RazorViewEngine
    {
        private void PrepareViewEngine()
        {
            this.AreaMasterLocationFormats = this.AreaMasterLocationFormats.Where(IsCSharpView).ToArray();
            this.AreaPartialViewLocationFormats = this.AreaPartialViewLocationFormats.Where(this.IsCSharpView).Select(this.PatchPartial).ToArray();
            this.AreaViewLocationFormats = this.AreaViewLocationFormats.Where(this.IsCSharpView).ToArray();
            this.FileExtensions = this.FileExtensions.Where(this.IsCSharpView).ToArray();
            this.MasterLocationFormats = this.MasterLocationFormats.Where(this.IsCSharpView).ToArray();
            this.PartialViewLocationFormats = this.PartialViewLocationFormats.Where(this.IsCSharpView).Select(this.PatchPartial).ToArray();
            this.ViewLocationFormats = this.ViewLocationFormats.Where(this.IsCSharpView).ToArray();

            this.templateLocationFormats = this.templateFolderNames.ToDictionary(
                item => item,
                item => this.PartialViewLocationFormats.Select(path => path.Replace("/_", "/" + item + "/_")).ToArray(),
                StringComparer.OrdinalIgnoreCase);

            this.areaTemplateLocationFormats = this.templateFolderNames.ToDictionary(
                item => item,
                item => this.AreaPartialViewLocationFormats.Select(path => path.Replace("/_", "/" + item + "/_")).ToArray(),
                StringComparer.OrdinalIgnoreCase);
        }

        private bool IsCSharpView(string path)
        {
            return !path.Contains(".vb");
        }

        private string PatchPartial(string path)
        {
            return path.Replace("/{0}.cshtml", "/_{0}.cshtml");
        }
    }

0
Comment actions Permalink

On 8/3/2012 12:47 AM, Robert Slaney wrote:

I take it that the R# system is trying to discover the location formats by code inspection.  Will that also work is the ViewEngines are declared in a reference assembly and not directly in the MVC web project ?  If so, that is surely an extremely fragile approach.


Yes, R# can detect view format changes only in source code and not in external binary assembly.
And, even in source code we can detect changes only by assigning of array of constants.

0
Comment actions Permalink

So the quick answer is that R# has hardcoded the view path format conventions and cannot be overriden, unless R# can detect an explicit set of the Properties on the view engine(s), which it can't do if not found in source code in the same project, or in an external assembly.

... so how can I turn OFF this feature until it's usable ?

0
Comment actions Permalink

On 08.08.2012 1:20, Robert Slaney wrote:

.. so how can I turn OFF this feature until it's usable ?


1. Disable views resolving only
Alt+Enter on unresolved view → Inspect options for "MVC (unknown view)" → Do not show

2. Disable all MVC functionality
ReSharper → Options → Code Editing → ASP.NET → MVC → Enable ASP.NET MVC References

0

Please sign in to leave a comment.