【问题标题】:Navigating between different Views from two different Application Projects within a Solution in MVC ASP.NET在 MVC ASP.NET 中的解决方案内的两个不同应用程序项目的不同视图之间导航
【发布时间】:2016-12-30 10:08:38
【问题描述】:

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

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

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

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

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

【问题讨论】:

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


【解决方案1】:
  1. 我通常使用区域来处理这个问题。

        var url = Url.Action("Index", "Home", new {Area = "Myarea"});
        var url = Url.Action("Index", "Home", new {Area = "area2"});
    
  2. 如果您喜欢这样添加其他项目,您可以使用自定义 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());
    }

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

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

【讨论】:

    【解决方案2】:

    您最好的选择可能是将条目放入两个具有两个站点根路径的 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);
    

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

    【讨论】:

    • 感谢您的回答,但我认为在我的情况下使用“区域”是最好的解决方案:)
    猜你喜欢
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多