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

1.1k views Asked by At

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

2

There are 2 answers

0
mohammadreza izadi On BEST ANSWER
  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.

1
Peter B On

Your best bet may be to just put entries in both Web.Config files with the root paths of both sites. Not something you'd want to hardcode anyway. Then use those to manipulate the strings that you get from Url.Action(), for which you can use any strings you want, it never checks if they actually match with your project.

Web.config (reverse entry values for the other site):

<appSettings>
    <add key="MyRoot" value="/Site1" />
    <add key="OtherRoot" value="/Site2" />
</appSettings>

Code:

var myRoot = WebConfigurationManager.AppSettings["MyRoot"];
var otherRoot = WebConfigurationManager.AppSettings["OtherRoot"];
var url = Url.Action("Bla", "YadaYadaYada");
var otherUrl = otherRoot + url.Substring(myRoot.Length);

You might want to create a helper or a singleton class per site etc. to optimize this, but the concept stays the same.