我有两个应用程序,一个用aspx web表单编写,另一个用于MVC5 razor cshtml。
网络表单中嵌入了iframe,我想在用户点击按钮时在iframe中加载razor 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
基于上面的帖子,我在一个名为Helpers的新文件夹下的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 });
}
}
}
在帖子中指定。然后我在models文件夹中创建了一个类(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();
}
}
然后我在views文件夹下创建了一个名为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.aspx~ / Views / Shared / PartialRender.ascx 〜/ Views / Dummy / PartialRender.cshtml~ / Views / Dummy / PartialRender.vbhtml 〜/查看/共享/ PartialRender.cshtml 〜/查看/共享/ PartialRender.vbhtml
当我尝试调试应用程序时,以下代码中的视图名称返回为null
ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
任何人都可以帮助我,让我知道我哪里错了吗?
答案 0 :(得分: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 }
);
}
}
我在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);
}
然后可以找到部分视图。但是接下来我遇到了另一个错误。
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很新,所以我希望我的术语合适。