gpt4 book ai didi

c# - 通过路径名返回 View

转载 作者:太空狗 更新时间:2023-10-30 00:10:53 26 4
gpt4 key购买 nike

我有一个具有基本功能的网站,但可以根据不同的客户和不同的合作伙伴进行覆盖。路由被设置为处理客户名称和合作伙伴名称作为路由的一部分:

   routes.MapRoute(
"DefaultRoute", // Route name
"{client}/{portal}/{controller}/{action}/{id}", // URL with parameters
new { client="UNKNOWN", portal="UNKNOWN", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Enterprise.Portal.Controllers" }
);

我有一个帮助程序类来确定是否存在将取代普通 View 的 View 。该站点有不同的客户,每个客户都有不同的合作伙伴。如果这些客户不想要默认 View ,他们可以提供 HTML,合作伙伴也可以这样做。我将这些备用 View 保存在一个文件夹中。帮助程序类获取信息,如果存在备用 View ,则将文件路径返回到该 View 。如果它返回 null 或空字符串,则使用普通 View 。

public static string ViewPath(string basePath, string client, string partner, string controller, string viewname)
// This returns something like C:\Sites\Portal\UI\ClientName\PartnerName\ControllerName\View.cshtml

在我的 Controller 中,如果返回非空值或空值,我如何提供要使用的 View 。这是我所做的,但不起作用:

        if (String.IsNullOrEmpty(this.model.CurrentViewLocation))
{
return View(model);
}
else
{
return View(this.model.CurrentViewLocation, model);
}

我收到以下错误,因为显然 return View() 构造函数不能使用路径名,只能使用 View 名。有没有办法做到这一点?如果需要,我可以将路径转换为虚拟 Web 路径,例如“~\UI\Client\Partner\Controller\View.cshtml”。

 Server Error in '/' Application

The view 'C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Account/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.aspx
~/Views/Account/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.ascx
~/Views/Shared/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.aspx
~/Views/Shared/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.ascx
~/Views/Account/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.cshtml
~/Views/Account/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.vbhtml
~/Views/Shared/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.cshtml
~/Views/Shared/C:\Sites\Portal\UI\ClientName\PartnerName\Account\LogOn.cshtml.vbhtml

我猜更好的方法是将 Client 文件夹和 Partner 文件夹添加到用于搜索 View 的 View 引擎的位置格式中。但格式字符串仅包含 Controller 的 {0} 和 View 名称的 {1}。我需要重写它以传递 Client 和 partner,它们都是通过路由传递的。

最佳答案

I can convert the paths to Virtual Web Paths if needed like "~\UI\Client\Partner\Controller\View.cshtml".

是的,这正是您应该做的,因为这是 View 方法所期望的 - 网站根目录的相对路径:

return View("~/UI/Client/Partner/Controller/View.cshtml", someViewModel);

关于c# - 通过路径名返回 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18490741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com