我有两个应用程序,一个是用aspx web窗体编写的,另一个是用MVC5 razor cshtml编写的。在Web窗体中共享Razor部分视图iFrame
Web窗体中嵌入了iframe,我想在用户单击按钮时在iframe中加载剃须刀cshtml文件。
我搜索并找到了一些有用的帖子来混合webforms和MVC页面,以便我可以在webforms aspx页面中显示MVC页面。
How to use ASP.Net MVC View inside WebForms .aspx page?
How to include a partial view inside a webform
基于上面的帖子,我在一个叫助手创建新文件夹在我的MVC应用程序中的MvcUtility类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Desk.Web.Client.Controllers;
using Desk.Web.Client.Models;
namespace Desk.Web.Client.Helpers
{
public class RazorPartialBridge
{
private static void RenderPartial(string partialViewName, object model)
{
HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Dummy");
ControllerContext controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new DummyController());
IView view = FindPartialView(controllerContext, partialViewName);
ViewContext viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output);
view.Render(viewContext, httpContextBase.Response.Output);
}
//Find the view, if not throw an exception
private static IView FindPartialView(ControllerContext controllerContext, string partialViewName)
{
ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
if (result.View != null)
{
return result.View;
}
StringBuilder locationsText = new StringBuilder();
foreach (string location in result.SearchedLocations)
{
locationsText.AppendLine();
locationsText.Append(location);
}
throw new InvalidOperationException(String.Format("Partial view {0} not found. Locations Searched: {1}", partialViewName, locationsText));
}
//Here the method that will be called from MasterPage or Aspx
public static void RenderAction(string controllerName, string actionName, object routeValues)
{
RenderPartial("PartialRender", new RenderActionViewModel() { ControllerName = controllerName, ActionName = actionName, RouteValues = routeValues });
}
}
}
在帖子中指定。然后创建了一个类(RendeActionViewModel)模型文件夹内,该代码是
namespace Desk.Web.Client.Models
{
public class RenderActionViewModel
{
public string ControllerName { get; set; }
public string ActionName { get; set; }
public object RouteValues { get; set; }
}
}
我所创建的文件夹的控制器
public class DummyController : Controller
{
public ActionResult PartialRender()
{
return PartialView();
}
}
然后下一个虚设控制器I创建下一个称为PartialRender.cshtml视图查看文件夹。
@model RenderActionViewModel
<h1>Hello World</h1>
<p>
I was rendered from a <strong>@Model.Source</strong>!
</p>
在我的asp.net webform应用程序的Webform中,我创建了一个新的aspx页面并添加了下面的代码。
<%@ Page Title="Demo" Language="C#"
AutoEventWireup="true" Inherits="Demo" Codebehind="Demo.aspx.cs" %>
<html>
<head></head>
<body>
<% RazorPartialBridge.RenderPartial("_Partial", new RenderActionViewModel() { Source = "ASPX Page" }) %>
</body>
</html>
然而,当我运行应用程序,我得到以下
局部视图PartialRender错误未找到。搜索位置: 〜/ Views/Dummy/PartialRender.aspx〜/ Views/Dummy/PartialRender.ascx 〜/ Views/Shared/PartialRender.ascpx〜/ Views/Shared/PartialRender.ascx 〜/ Views/Dummy/PartialRender。 CSHTML〜/查看/假/ PartialRender.vbhtml 〜/查看/共享/ PartialRender.cshtml 〜/查看/共享/ PartialRender.vbhtml
,并在下面的代码时的视图名被返回为空我试图调试应用程序
ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
任何人都可以请帮助我,让我知道在哪里 我错了?
感谢您的信息。我会试试看。 –