在WebForms iFrame中共享剃刀局部视图 [英] Share Razor Partial View in WebForms iFrame
问题描述
我有两个应用程序,一个用aspx web形式编写,另一个用MVC5剃须刀cshtml编写.
I have two applications one written in aspx web form and another in MVC5 razor cshtml.
该网络表单中嵌入了一个iframe,当用户单击按钮时,我想在iframe中加载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.
我搜索并找到了一些有用的帖子,将Web表单和MVC页面混合在一起,以便可以在Webforms aspx页面中显示MVC页面.
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.
如何使用WebForms .aspx页内的ASP.Net MVC视图?
基于上述内容,我在MVC应用程序中的一个名为Helpers的新文件夹下创建了一个MvcUtility类.
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 });
}
}
}
在帖子中指定.然后我在models文件夹中创建了一个类(RendeActionViewModel),代码为
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; }
}
}
我在controllers文件夹下创建了一个虚拟控制器
I created a dummy controller under the controllers folder
public class DummyController : Controller
{
public ActionResult PartialRender()
{
return PartialView();
}
}
然后我在views文件夹下创建了一个名为PartialRender.cshtml的视图.
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>
在我的asp.net网络表单应用程序的网络表单中,我创建了一个新的aspx页面,并添加了以下代码.
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
找不到局部视图PartialRender.搜寻地点:〜/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
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
当我尝试调试应用程序时,在下面的代码中视图名称被返回为null
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?
推荐答案
我刚刚按照与您在上面提到的相同的指南遇到了相同的问题.
I've just ran into the same issue following the same guides as you included above.
我相信原因是我的解决方案在MVC领域具有意见.我在这里使用@devundef的答案是否可以在ASP.NET MVC中指定一个自定义位置来搜索视图"?来解决找到局部视图.
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.
我的区域注册如下:
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 }
);
}
}
在Global.asax中有以下内容
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.
我发现我需要将区域"添加到路线数据中,如下所示:
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);
}
我对MVC还是很陌生,所以我希望我的术语正确.
I'm fairly new to MVC so I hope I got my terminology right.
这篇关于在WebForms iFrame中共享剃刀局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!