2016-07-07 40 views
0

我有两个应用程序,一个是用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); 

任何人都可以请帮助我,让我知道在哪里 我错了?

回答

0

我刚刚遇到同样的问题,遵循上面包含的相同指南。

我相信原因是我的解决方案在MVC领域有意见。我使用@ devundef的答案Can I specify a custom location to “search for views” in ASP.NET MVC?来解析定位局部视图。

我区注册如下:

public class MVCAreaRegistration : AreaRegistration 
{ 
    public override string AreaName 
    { 
     get 
     { 
      return "MVC"; 
     } 
    } 

    public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      "MVC_default", 
      "MVC/{controller}/{action}/{id}", 
      new { action = "Index", id = UrlParameter.Optional } 
     );   
    } 
} 

而在全球,我有以下。asax

protected void Application_Start() 
    { 
     GlobalConfiguration.Configure(WebApiConfig.Register); 
     AreaRegistration.RegisterAllAreas(); 
     ViewEngines.Engines.Clear(); 
     var razorEngine = new RazorViewEngine(); 
     razorEngine.MasterLocationFormats = razorEngine.MasterLocationFormats 
       .Concat(new[] { 
     "~/Areas/MVC/{0}.cshtml" 
       }).ToArray(); 

     razorEngine.PartialViewLocationFormats = razorEngine.PartialViewLocationFormats 
       .Concat(new[] { 
     "~/Areas/MVC/Views/{1}/{0}.cshtml", 
     "~/Areas/MVC/Views{0}.cshtml",   
       }).ToArray(); 

     ViewEngines.Engines.Add(razorEngine); 
    } 

继而可以找到局部视图。但之后我遇到了另一个错误。

No route in the route table matches the supplied values.

我发现我需要的“区域”添加到路由数据如下:

private static void RenderPartial(string partialViewName, object model) 
    { 
     HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current); 
     RouteData routeData = new RouteData(); 
     routeData.Values.Add("controller", "WebFormsUtility"); 
     routeData.Values.Add("area", "MVC"); //Added area to routeData 
     ControllerContext controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new WebFormsUtilityController()); 
     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); 
    } 

我是相当新的MVC所以我希望我得到了我的术语的权利。

+0

感谢您的信息。我会试试看。 –