如何分配给 WebPage 或 WebPageBase 的 UrlData 和 PageData 属性?我正在尝试编写一个自定义路由处理程序,它说属性是只读的。这就是我想要完成的事情:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Routing;
using System.Web.WebPages;
/// <summary>
/// Summary description for CustomRazorRouteHandler
/// </summary>
public class CustomRazorRouteHandler : IRouteHandler
{
public string VirtualPath { get; set; }
public CustomRazorRouteHandler(string virtualPath)
{
this.VirtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var page = WebPage.CreateInstanceFromVirtualPath(VirtualPath);
foreach (var item in requestContext.RouteData.Values)
{
page.PageData[item.Key] = item.Value; // Null reference exception
page.UrlData.Add(item.Value.ToString()); // Read-only error
}
var handler = WebPageHttpHandler.CreateFromVirtualPath(VirtualPath);
return handler;
}
}
该网络应用程序适用于中等信任,因此反射并不是一个真正的选择。