繁体   English   中英

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

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

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

  1. DAL
  2. BLL
  3. 商业实体
  4. 应用1
  5. 应用2

现在,我想知道如何在这两个应用程序的视图之间导航。通常,我们执行以下操作以在应用程序中导航

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

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

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

第二:添加您的应用程序的虚拟路径:

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

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

如果您愿意,可以开发CustomAreaViewEngine,该应用程序可以将您的应用程序放置到MyModules等自定义目录中。

最好的选择是将条目与两个站点的根路径一起放入两个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);

您可能希望在每个站点等上创建一个帮助程序或单例类,以对其进行优化,但是概念保持不变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM