2016-07-07 68 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

感謝您的信息。我會試試看。 –

相關問題