【发布时间】:2014-09-22 15:44:30
【问题描述】:
我一直在研究 MVC 视图和局部视图的动态内容,但没有成功找到适合我需要的架构。
基本上,我需要根据 URL 传递的参数创建登录页面。 基础知识 http://mydns.com/myconroller/myview/?landingpage=Param1
控制器需要找到用于创建视图的 HTML。 根据登录页面,视图将有所不同。 (为了这个问题,我以登陆页面为例) 我的目标是能够部署一个登陆页面,并基于 URL 使用该 HTML 登陆页面在视图中基于传递的landingpage 参数。
控制器中当前还有其他正在运行的视图。我正在尝试添加功能,以便能够添加新的一次性页面而无需重新编译。
我已经搜索了有关如何加载动态视图的各种想法,但根据我所阅读的内容,似乎无法找到适合此需求的解决方案。
我可能会 RedirectToAction,但我仍然不知道在哪里部署,我遇到了 Razor 的几个问题,因为它不在共享目录中,然后我遇到了部署问题,因为我想组织登录页面与我组织视图不同。
解决方案: 我决定采用不同的方法并在控制器中使用 ContentResult Action。我仍然有主视图,并且我使用 HTML 扩展来呈现我在客户目录中部署的 HTML 页面。
@{
Html.RenderAction("LandingPageContent", "Controller", Model);
}
然后在控制器中直接加载 HTML 并返回 ContentResult
public ContentResult LandingPageContent(object model, FormCollection collection)
{
MySRCHelper helper = new MySRCHelper();
ContentVariables variables = helper.getContentSRC(model.EntryCode);
model.ContentSRC = variables.LandingPageSRC;
return Content(System.IO.File.ReadAllText(Server.MapPath(model.ContentSRC)));
}
然后我可以配置要使用的原始 HTML 文件的路径,并将其加载到视图中。然后,视图可以容纳加载 jQuery、CSS 和其他必要的 javascript 以与原始 HTML 集成的所有路径,并允许我将 HTML 文件部署到我想要的任何目录结构中。配置 XML 文件允许我查找 XML 元素并将这些值用于我正在寻找的任何 HTML,例如欢迎和感谢页面。助手对象将打开 XML 并根据传递给视图的参数查找配置。
<ContentLandingItem entrycode="1" customerID="Cutomer1">
<ContentLandingPageSRC>~/Customers/Customer1/Customer1Landing.htm</ContentLandingPageSRC>
<ContentThankyouSRC>~/Content/Default/GenericThankyou.htm</ContentThankyouSRC>
</ContentLandingItem>
<ContentLandingItem entrycode="2" customerID="Cutomer2">
<ContentLandingPageSRC>~/Customers/Customer2/Customer2Landing.htm</ContentLandingPageSRC>
<ContentThankyouSRC>~/Customers/Customer2/Customer2Thankyou.htm</ContentThankyouSRC>
</ContentLandingItem>
视图仍然执行其职责并独立工作,让原始 HTML 装饰视图。该模型仍然完好无损,可以随心所欲地使用。 FormCollection 是为了防止表单提交将值发布到视图并提供一些我在这个问题中省略的内容,因为它与这个主题无关。
我不想回答我自己的问题,我在另一个网站上找到了对我有帮助的文章,所以我把我所做的放在这里,以防有人需要这个功能。
【问题讨论】:
-
感谢您的帮助,我决定使用 HTML 扩展并将文件加载到视图中。
标签: html asp.net-mvc razor model-view-controller