本文介绍了如何分配到PageData和UrlData在自定义路由处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何分配到网页或WebPageBase的UrlData和PageData属性?我试着写一个自定义的路由处理程序和它说,该属性是只读的。这就是我要完成的:

How do you assign to the UrlData and PageData properties of WebPage or WebPageBase? I'm trying to write a custom route handler and it says that the properties are read-only. This is what I'm trying to accomplish:

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;
    }
}

Web应用程序是为中等信任,这样的反思是不是一个真正的选择。

The web app is meant for Medium Trust, so Reflection is not really an option.

推荐答案

看来是没有办法分配PageData和UrlData无反射,因为列表标记为只读。我的解决办法是创建一对夫妇的URL peices存储在HttpContext的帮手和读他们退了出去。

Seems there is no way to assign PageData and UrlData without reflection because the lists are marked as read-only. My work around is to create a couple of helpers that store the url peices in the HttpContext and read them back out.

这篇关于如何分配到PageData和UrlData在自定义路由处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 03:03