Share Razor Partial View in WebForms iFrame

I have two applications written in an aspx web form, and the other in MVC5 razor cshtml.

An iframe is embedded in the web form, and I want to load the cztml razor file inside the iframe when the user clicks the button.

I searched and found useful posts for mixing webforms and MVC pages so that I can display the MVC page on an aspx webforms page.

How to use ASP.Net MVC View inside WebForms.aspx page?

How to enable partial view inside web form

Based on the above post, I created the MvcUtility class in my MVC application in a new folder called Helpers.

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 }); } } } 

as indicated in the message. Then I created a class (RendeActionViewModel) inside the models folder, and the code

 namespace Desk.Web.Client.Models { public class RenderActionViewModel { public string ControllerName { get; set; } public string ActionName { get; set; } public object RouteValues { get; set; } } } 

I created a dummy controller under the controllers folder

 public class DummyController : Controller { public ActionResult PartialRender() { return PartialView(); } } 

Then I created a view called PartialRender.cshtml in the views folder.

  @model RenderActionViewModel <h1>Hello World</h1> <p> I was rendered from a <strong>@Model.Source</strong>! </p> 

In the webform of my asp.net web application, I created a new aspx page and added the code below.

  <%@ 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> 

However, when I launch the application, I get the error below

Partial view of PartialRender not found. Search for places: ~ / Views / Dummy / PartialRender.aspx ~ / Views / Dummy / PartialRender.aspx ~ / Views / Shared / PartialRender.aspx ~ / Views / Shared / PartialRender.ascx ~ / Views / Dummy / PartialRender.cshtml ~ / Views / Dummy / PartialRender.vbhtml ~ / Views / Shared / PartialRender.cshtml ~ / Views / Shared / PartialRender.vbhtml

and the view name is returned as null in the code below when I tried to debug the application

 ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName); 

Can someone help me and tell me where I am going wrong?

0
c # asp.net-mvc razor
Jul 07 '16 at 9:15
source share
1 answer

I ran into the same problem following the same guidelines as above.

I believe the reason is that my solution has insights in the field of MVC. I used the @ devundef answer here. Can I specify a custom location for "view searches" in ASP.NET MVC? to allow localization of a partial view.

My areas are registered as follows:

 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 } ); } } 

And I have the following in Global.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); } 

After that, you can now find a partial view. But after that I came across another error.

No route in the route table matches the specified values.

I found that I need to add "area" to the route data as follows:

 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); } 

I am new to MVC, so I hope I got my terminology right.

0
Jul 27 '16 at 11:52
source share



All Articles