I added a default MvcApplication (MVC 4) (its name is MvcApplication3 matching the name of my solution's) width Home views (About, Index, Contact) and that will be my startup (bold in VS solution explorer's interface) project. Then I added another project (MvcApplication, but this time an empty one) called MvcApplication2 to the solution. Then I added the latter project as a reference to the first. I also added a controller called TestController (green line) to the referenced project and generated a view for its Index (red arrow) method. However, when I go to a link /Test or /Test/Index, the view I am expecting (red arrow) is not shown. Then I added the same folder Test with Index.cshtml (blue arrow) to the main project and now I am seeing its contents rather than the project's where my controller sits in.

Is it possible to make the application look for the views in the other project rather than the startup one?

I am adding the image of the structure to make it easier to follow.

P.S.: probably related: the breakpoint IS being hit in the Index method of TestController.

tldr; blue view is used instead of a red one

enter image description here

有帮助吗?

解决方案 3

With some ideas from me and a friend of mine and a link about overriding RazorViewEngine I finally got what I wanted working exactly how I was expecting it to:

  1. I created a folder named ViewsBase in the main project.
  2. I rewrote RazorViewEngine this way: I only changed the place that was needed for me, leaving everything else like I found in the constructor of RazorViewEngine:

    public class MyCustomViewEngine : RazorViewEngine { public MyCustomViewEngine() { ...

         ViewLocationFormats = new[]
         {
              "~/Views/{1}/{0}.cshtml",
              "~/Views/{1}/{0}.vbhtml",
              "~/Views/Shared/{0}.cshtml",
              "~/Views/Shared/{0}.vbhtml",
              "~/ViewsBase/{1}/{0}.cshtml",
              "~/ViewsBase/{1}/{0}.vbhtml"
         };
    
        ...
    

(I find it rather disturbing how I am unable to format the code properly. Can someone give me a hand please?)

and in Global.asax of the main project I added:

ViewEngines.Engines.Clear();
var ourViewEngine = new 
ViewEngines.Engines.Add(ourViewEngine);

AreaRegistration.RegisterAllAreas();
...
  1. I added a post-build event command:

    xcopy /s "$(ProjectDir)Views\*.*" /Y "$(SolutionDir)$(SolutionName)\ViewsBase\"

  2. It started looking for the views in the expected order

  3. I added a ViewStart file to the base project to make it render the Layout too.

其他提示

I think your problema is that you have set MvcApplication3 as startup Project and is causing you to open the view off that Project.

Is it possible to make the application look for the views in the other project rather than the startup one?

Yes its posible, you can redirect your application the url. Think this your application have a url http://localhost:(someport) you can set redirect to the port of the second application.

I put a link to for better understanding a routing system of MVC: Documentation of routing system

As far as I know, you can't link to projects together like that. Each project becomes its own website with its own address. The reason you might put multiple projects together in one solution is to share things like classes, services, etc. I think what you're needing is areas:

http://www.codeproject.com/Articles/714356/Areas-in-ASP-NET-MVC

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top