Search code examples
c#asp.netasp.net-mvcrazorn-tier-architecture

Navigating between different Views from two different Application Projects within a Solution in MVC ASP.NET


I have a Project going on in which I will have to develop Two Applications. I am using n-tier Artitecture and have following Projects in A Solution

  1. DAL
  2. BLL
  3. BusinessEntities
  4. Application1
  5. Application2

Now I want to Know How can I navigate between the views of these two Applications Normally we do the following to navigate within an Application

@Url.Action("ActionName", "ControllerName", OtherStuff..)

Now How can I navigate from action of one Application to action in Second Application. Thanks


Solution

    1. i usually use areas for handle this.

          var url = Url.Action("Index", "Home", new {Area = "Myarea"});
          var url = Url.Action("Index", "Home", new {Area = "area2"});
      
    2. if u like your add other projects like this way u can use Custom ViewEngine. in this way first add route rules like this :

      routes.MapRoute(
             name: "app",
             url: "{application}/{controller}/{action}/{id}",
             defaults: new {application = "MyApplication1", controller = "Panel", action = "Index", id = UrlParameter.Optional }
             );
      

    second: add virtual paths of ur app :

     public class CustomAreaViewEngine : VirtualPathProviderViewEngine
    {
        public CustomAreaViewEngine()
        {
            MasterLocationFormats = new string[]
            {
                "~/Views/{1}/{0}.master",
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.master",
            "~/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.master",
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.master",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/{2}/Views/{1}/{0}.master",
            "~/Areas/{2}/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/{2}/Views/Shared/{0}.master",
            "~/Areas/{2}/{2}/Views/Shared/{0}.cshtml",
            "~/{2}/Views/{1}/{0}.master",
            "~/{2}/Views/{1}/{0}.cshtml",
            "~/{2}/Views/Shared/{0}.master",
            "~/{2}/Views/Shared/{0}.cshtml",
            "~/{2}/{2}/Views/{1}/{0}.master",
            "~/{2}/{2}/Views/{1}/{0}.cshtml",
            "~/{2}/{2}/Views/Shared/{0}.master",
            "~/{2}/{2}/Views/Shared/{0}.cshtml",
    
            };
            ViewLocationFormats = new string[]
            {
            "~/Areas/{2}/Views/{1}/{0}.aspx",
            "~/Areas/{2}/Views/{1}/{0}.ascx",
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.aspx",
            "~/Areas/{2}/Views/Shared/{0}.ascx",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/{2}/Views/{1}/{0}.aspx",
            "~/Areas/{2}/{2}/Views/{1}/{0}.ascx",
            "~/Areas/{2}/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/{2}/Views/Shared/{0}.aspx",
            "~/Areas/{2}/{2}/Views/Shared/{0}.ascx",
            "~/Areas/{2}/{2}/Views/Shared/{0}.cshtml",
            "~/{2}/Views/{1}/{0}.aspx",
            "~/{2}/Views/{1}/{0}.ascx",
            "~/{2}/Views/{1}/{0}.cshtml",
            "~/{2}/Views/Shared/{0}.aspx",
            "~/{2}/Views/Shared/{0}.ascx",
            "~/{2}/Views/Shared/{0}.cshtml",
            "~/{2}/{2}/Views/{1}/{0}.aspx",
            "~/{2}/{2}/Views/{1}/{0}.ascx",
            "~/{2}/{2}/Views/{1}/{0}.cshtml",
            "~/{2}/{2}/Views/Shared/{0}.aspx",
            "~/{2}/{2}/Views/Shared/{0}.ascx",
            "~/{2}/{2}/Views/Shared/{0}.cshtml",
            "~/Views/{1}/{0}.aspx",
            "~/Views/{1}/{0}.ascx",
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.aspx",
            "~/Views/Shared/{0}.ascx",
            "~/Views/Shared/{0}.cshtml"
            };
            PartialViewLocationFormats = ViewLocationFormats;
        }
    

    and u sould change global.asax :

     protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
    
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new CustomAreaViewEngine());
        }
    

    and finally u should implement ur controller in main application namespace. Do you still need to explain this way?

    if u want u can develop CustomAreaViewEngine that can put ur applications to a custom directory like MyModules.