我有兩個應用程序,一個是用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);
任何人都可以請幫助我,讓我知道在哪裏 我錯了?
感謝您的信息。我會試試看。 –