Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access MVC Views located in another project?

I want to separate my MVC project into several projects

So first of all, I've created two projects Front and Views

The Front project is a web application that contains controllers and models

The Views project is a class library project that will contains only the views

My question is how can I make controllers call views located in the Views project

I have controllers like this one:

public ActionResult Default()         {             return this.View();         } 
like image 271
Mehdi Souregi Avatar asked Jun 21 '14 11:06

Mehdi Souregi


People also ask

Can two different controllers access a single view in MVC?

Yes. Mention the view full path in the View method. If the name of your Views are same in both the controllers, You can keep the Common view under the Views/Shared directory and simply call the View method without any parameter.

Is MVC a cross platform?

The main difference between ASP.NET Core and ASP.NET MVC 5 is their cross-platform approach. ASP.NET Core can be used on Windows, Mac, or Linux, whereas ASP.NET MVC 5 can only be used for applications on Windows.

What is shared view in MVC?

If a view isn't found in this location, then by convention the MVC runtime looks in the “Views\Shared”. This simple organization scheme works well for small projects, but can become quite unwieldy as the size of the web site grows and the shared folder becomes an ever larger dumping ground.


1 Answers

For including controllers you need to change your route registrations to tell them where to look for the controllers:

routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}",                 namespaces: new[] {"[Namespace of the Project that contains your controllers]"},                 defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}); 

For including views, create custom ViewEngine:

public class CustomViewEngine: RazorViewEngine {     public CustomViewEngine()     {         MasterLocationFormats = new string[]         {             "~/bin/Views/{1}/{0}.cshtml",             "~/bin/Views/{1}/{0}.vbhtml",             "~/bin/Views/Shared/{0}.cshtml",             "~/bin/Views/Shared/{0}.vbhtml"          };         ViewLocationFormats = new string[]         {              "~/bin/Areas/{2}/Views/{1}/{0}.cshtml",              "~/bin/Areas/{2}/Views/{1}/{0}.vbhtml",              "~/bin/Areas/{2}/Views/Shared/{0}.cshtml",              "~/bin/Areas/{2}/Views/Shared/{0}.vbhtml"         };         .         .         .     } } protected void Application_Start() {     ViewEngines.Engines.Add(new CustomViewEngine()); 

For more information look at the default implementation of RazorViewEngin.

Here some good articles:

A Custom View Engine with Dynamic View Location

Using controllers from an external assembly in ASP.NET Web API

How to call controllers in external assemblies in an ASP.NET MVC application

How do I implement a custom RazorViewEngine to find views in non-standard locations?

Views in separate assemblies in ASP.NET MVC

like image 133
Mohsen Esmailpour Avatar answered Sep 19 '22 03:09

Mohsen Esmailpour