在MVC ASP.NET的解决方案中从两个不同的应用程序项目中的不同视图之间导航

时间:2016-12-30 10:08:38

标签: c# asp.net asp.net-mvc razor n-tier-architecture

我正在进行一项项目,我将不得不开发两个应用程序。我正在使用n层Artitecture并在解决方案中拥有以下项目

  1. DAL
  2. BLL
  3. BusinessEntities
  4. 应用1
  5. 应用2
  6. 现在我想知道如何在这两个应用程序的视图之间导航通常我们在应用程序中进行以下操作

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

    现在,我如何从一个应用程序的操作导航到第二个应用程序中的操作。 感谢

2 个答案:

答案 0 :(得分:2)

  1. 我通常使用区域来处理这个问题。

        var url = Url.Action("Index", "Home", new {Area = "Myarea"});
        var url = Url.Action("Index", "Home", new {Area = "area2"});
    
  2. 如果您喜欢以这种方式添加其他项目,则可以使用Custom ViewEngine。 以这种方式首先添加这样的路由规则:

    routes.MapRoute(
           name: "app",
           url: "{application}/{controller}/{action}/{id}",
           defaults: new {application = "MyApplication1", controller = "Panel", action = "Index", id = UrlParameter.Optional }
           );
    
  3. 第二: 添加你的应用的虚拟路径:

     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;
        }
    

    你可以改变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());
        }
    

    最后你应该在主应用程序命名空间中实现你的控制器。 你还需要这样解释吗?

    如果你想要你可以开发CustomAreaViewEngine,可以将你的应用程序放到像MyModules这样的自定义目录。

答案 1 :(得分:0)

您最好的选择可能是将两个Web.Config文件中的条目与两个站点的根路径放在一起。不管怎样你都不想硬编码。然后使用它们来操作从Url.Action()获得的字符串,为此你可以使用你想要的任何字符串,它永远不会检查它们是否与你的项目实际匹配。

Web.config (其他网站的反向条目值):

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

<强>代码:

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

您可能希望为每个站点等创建一个帮助程序或单个类来优化它,但概念保持不变。