12

I have two MVC projects one as a parent project and the other as a child project. The child project adds reference to the parent project. I like to use partial views from the parent project from the child project something like -

@Html.Partial("_GenericGreeting") <-- in child project

_GenericGreeting.cshtml <-- is in parent project

The child project is the project that starts up. Parent project i mean is like a base/shared project. RazorGenerator has been installed on both projects and each projects can be compiled into single assembles.

When I ran the project, i'm only getting this following error.

The partial view '_GenericGreeting' was not found or no view engine supports the searched locations.

If i copy the partial view and paste it into the child project it's fine but i don't want to duplicate the files.

I have tried this post but no luck but maybe i'm not adding it right.

Community
  • 1
  • 1
Laurence
  • 7,633
  • 21
  • 78
  • 129
  • Can you show your code on what you tried for implementing class CustomViewEngine and global.asax Application_OnStart() ? It will be different from your linked post for Razor. – Raul Nohea Goodness Aug 10 '15 at 16:38
  • Hi @RaulNoheaGoodness I don't have that code anymore .. I just tried that quickly and didn't work so i deleted it .. But i fixed my problem tho. I created an answer with the fix i have. Thanks. – Laurence Aug 11 '15 at 15:26

2 Answers2

6

Not really good, but simple solution that can solve your problem. One of overloads @Html.Partial() allows you to write full path to your View.

Something like this:

@Html.Partial("~/View/Shared/_GenericGreeting.cshtml")
teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • Thanks @teo .. this might be a silly question but can you give me an example here if i want to link to different project how do i write the full path in my case? – Laurence Aug 10 '15 at 15:12
  • @lawphotog if you want to change directory to parent foulder use `..` like this: `@Html.Partial("~/../../OtherProjectFolder/View/Shared/_GenericGreeting.cshtml")`. But make sure that your process that run your site have permissions to this folder. – teo van kot Aug 11 '15 at 06:51
3

I accepted teo's answer. He helped me and gave me clue to the problem. But there are few important things to check.

One thing I wasn't aware is the generated cshtml files have properties called PageVirtualPathAttribute. One problem i couldn't get the right combination is because i was getting the path wrong.

Another thing to look is this class which is auto generated when you installed RazorGenerator and it is in your App_Start folder.

public static class RazorGeneratorMvcStart {
        public static void Start() {
            var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly) {
                UsePhysicalViewsIfNewer = HttpContext.Current.Request.IsLocal
            };

            ViewEngines.Engines.Insert(0, engine);

            // StartPage lookups are done by WebPages. 
            VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);
        }
    }

However this line has an overload method to define path to your compiled view. My shared project was created by someone else and he put this path in it.

var engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly, @"~/Areas/Cart/", null)

Once I have those bits checked, I just need to use like the following in my view as teo suggested.

@Html.Partial("~/Areas/Cart/Views/Home/_GenericGreeting.cshtml")
Laurence
  • 7,633
  • 21
  • 78
  • 129