簡體   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