Share Razor Partial View in WebForms iFrame

2019-09-14 22:32发布

问题:

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?

回答1:

I've just ran into the same issue following the same guides as you included above.

I believe the reason is my solution has the views in a MVC area. I used @devundef's answer here Can I specify a custom location to “search for views” in ASP.NET MVC? to resolve locating the partial view.

My areas is 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);
    }

Following that the partial view can now be located. But following that I ran into another error.

No route in the route table matches the supplied values.

I found I needed to add the "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'm fairly new to MVC so I hope I got my terminology right.