I have two applications one written in aspx web form and another in MVC5 razor cshtml.
The web form has an iframe embedded in it and I want to load the razor cshtml file inside the iframe when the user clicks a button.
I searched and found some helpful posts to mix webforms and MVC pages so that I can show the MVC page in the webforms aspx page.
How to use ASP.Net MVC View inside WebForms .aspx page?
How to include a partial view inside a webform
based on the above post, I created a MvcUtility class in my MVC application under 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 specified in the post. Then I created a class (RendeActionViewModel ) inside the models folder and the code is
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 under 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 webform application I created a new aspx page and added the below code.
<%@ 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 run the application, I am getting the error below
Partial view PartialRender not found. Locations Searched: ~/Views/Dummy/PartialRender.aspx ~/Views/Dummy/PartialRender.ascx ~/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 being returned as null in the below code when i tried to debug the application
ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
Can anyone please help me and let me know where I am wrong?