通过路径名返回视图 [英] Returning view via a path name

查看:103
本文介绍了通过路径名返回视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  routes.MapRoute(
                默认路由,//路线名称
                {客户} / {门户} / {控制器} / {行动} / {ID},// URL带参数
                新{客户=未知,门户=未知,控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
                新的String [] {Enterprise.Portal.Controllers}
            );

我有一个辅助类,以确定是否认为存在将取代普通视图。该网站有不同的客户,每个客户都有不同的合作伙伴。这些客户端可以提供HTML,如果他们不想要的默认视图,以及合作伙伴可以做同样的。我把一个文件夹中的这些替代的观点。辅助类取信息,如果不同的观点存在,返回到该视图中的文件路径。如果返回null或空字符串,普通视图中。

 公共静态字符串ViewPath(字符串基本路径,字符串的客户,合作伙伴串,串控制器,串视图名)
//这将返回类似于C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\ ControllerName \\ View.cshtml

在我的控制器,如果返回非null或空值,如何提供该视图中使用。下面是我做的,它不工作:

 如果(String.IsNullOrEmpty(this.model.CurrentViewLocation))
        {
            返回查看(模型);
        }
        其他
        {
            返回查看(this.model.CurrentViewLocation,模型);
        }

我收到以下错误,因为很明显,返回查看()构造函数无法使用路径名,仅视图名称。有没有办法做到这一点?我可以根据需要像〜\\ UI \\客户端\\合作伙伴\\控制器\\ View.cshtml的路径转换为虚拟网络路径。

 在'/'Application Server的错误视图'C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\帐户\\ LogOn.cshtml'或它的主人没有被发现或没有视图引擎支持搜索到的位置。在以下地点被搜查:
〜/查看/帐号/ C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\帐户\\ LogOn.cshtml.aspx
〜/查看/帐号/ C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\帐户\\ LogOn.cshtml.ascx
〜/查看/共享/ C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\帐户\\ LogOn.cshtml.aspx
〜/查看/共享/ C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\帐户\\ LogOn.cshtml.ascx
〜/查看/帐号/ C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\帐户\\ LogOn.cshtml.cshtml
〜/查看/帐号/ C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\帐户\\ LogOn.cshtml.vbhtml
〜/查看/共享/ C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\帐户\\ LogOn.cshtml.cshtml
〜/查看/共享/ C:\\网站\\门户\\ UI \\ CLIENTNAME \\ PartnerName \\帐户\\ LogOn.cshtml.vbhtml

我猜一个更好的方式做这将是客户端文件夹和合作伙伴文件夹添加到这是用来搜索的视图的视图引擎的位置格式。但是,格式字符串只包含{0}控制器和{1}的视图名。我需要重写它通过客户和合作伙伴,以及,这​​是通过两个途径传递。


解决方案

  

如果需要像我可以转换的路径虚拟Web路径
  〜\\ UI \\客户端\\合作伙伴\\控制器\\ View.cshtml。


是的,那是precisely你应该做的,因为这是在查看方法需要 - 一个相对路径的网站的根目录:

 返回查看(〜/ UI /客户/合作伙伴/控制器/ View.cshtml,someViewModel);

I have a site that has basic functionality, but can be overriden based on different clients and different partners. The Routing is set up to handle the client name and the partner name as part of the route:

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

I have a helper class to determine if a view exists that will supercede the normal view. The site has different clients and each client has different partners. These clients can provide HTML if they do not want the default views, and the partners can do the same. I keep these alternate views in a folder. The helper class takes the information and if an alternate view exists, returns the file path to this view. If it returns null or empty string, the normal view is used.

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

In my controller, if this returns a non null or empty value, How do I provide that view to be used. Here is what I did, which doesn't work:

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

I am getting the following error, because obviously the return View() constructor can not use path names, only View names. Is there a way to accomplish this? I can convert the paths to Virtual Web Paths if needed like "~\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

I'm guessing a better way to do this would be to add the Client folder and the Partner folder to the Location Formats of the view engine which are used searched for the views. But the format string only includes {0} for the controller and {1} for the viewname. I would need to override it to pass the Client and the partner as well, which are both passed via the route.

解决方案

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

Yes, that's precisely what you should do because that's what the View method expects - a relative path to the root of the website:

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

这篇关于通过路径名返回视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆