在 MVC 解决方案中的两个不同应用程序项目的不同视图之间导航 ASP.NET
Navigating between different Views from two different Application Projects within a Solution in MVC ASP.NET
我正在进行一个项目,我必须在其中开发两个应用程序。我正在使用 n 层 Artitecture 并在解决方案中有以下项目
- 达尔
- BLL
- 商业实体
- 申请1
- 申请2
现在我想知道如何在这两个应用程序的视图之间导航通常我们执行以下操作以在应用程序中导航
@Url.Action("ActionName", "ControllerName", OtherStuff..)
现在如何从一个应用程序的操作导航到第二个应用程序中的操作。
谢谢
您最好的选择可能是将条目放在两个 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);
您可能想为每个站点创建一个助手或一个单例 class 来优化它,但概念保持不变。
我通常使用区域来处理这个。
var url = Url.Action("Index", "Home", new {Area = "Myarea"});
var url = Url.Action("Index", "Home", new {Area = "area2"});
如果您喜欢以这种方式添加其他项目,您可以使用自定义 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。
我正在进行一个项目,我必须在其中开发两个应用程序。我正在使用 n 层 Artitecture 并在解决方案中有以下项目
- 达尔
- BLL
- 商业实体
- 申请1
- 申请2
现在我想知道如何在这两个应用程序的视图之间导航通常我们执行以下操作以在应用程序中导航
@Url.Action("ActionName", "ControllerName", OtherStuff..)
现在如何从一个应用程序的操作导航到第二个应用程序中的操作。 谢谢
您最好的选择可能是将条目放在两个 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);
您可能想为每个站点创建一个助手或一个单例 class 来优化它,但概念保持不变。
我通常使用区域来处理这个。
var url = Url.Action("Index", "Home", new {Area = "Myarea"}); var url = Url.Action("Index", "Home", new {Area = "area2"});
如果您喜欢以这种方式添加其他项目,您可以使用自定义 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。