【问题标题】:Create Dynamic View with static HTML in MVC在 MVC 中使用静态 HTML 创建动态视图
【发布时间】: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 asp.net-mvc razor model-view-controller


【解决方案1】:

这听起来像是使用您可以从虚拟路径提供程序视图引擎继承并根据 URL 参数(或其他)决定返回哪个视图。您可以根据需要进行调整的一些示例:

public class CustomViewEngine : VirtualPathProviderViewEngine
{
   public MyViewEngine()
   { 
      this.ViewLocationFormats = new string[] { "~/Views/{1}/{2}.mytheme ", "~/Views/Shared/{2}.mytheme" };
      this.PartialViewLocationFormats = new string[] { "~/Views/{1}/{2}.mytheme ", "~/Views/Shared/{2}. mytheme " };
   }
   protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
   {
      var physicalpath = controllerContext.HttpContext.Server.MapPath(partialPath);
      return new RazorView(controllerContext, physicalpath);
   }
   protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
   {
      var physicalpath = controllerContext.HttpContext.Server.MapPath(viewPath);
      return new RazorView(controllerContext, physicalpath);
   }
}

在那里你可以返回一个 RazorView 或 WebFormView 并设置你想要的路径供视图使用。

【讨论】:

  • 个别视图不起作用,但我喜欢 CustomViewEngine 的想法并将其用于其他事情。我需要在不创建新视图或继承任何 MVC 对象的情况下使用简单的 xCopy 部署到目录。我得到的 HTML 只是普通的 jane vanilla HTML,根本没有任何功能。
  • 那么您可以返回的不是剃刀视图,而是一个自定义视图,除了加载纯 html 文件并显示它之外什么都不做(或做一些自定义字符串替换或任何您想要的)
  • 我决定使用与编辑问题不同的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-25
  • 1970-01-01
相关资源
最近更新 更多