简体   繁体   中英

ASP.NET Response.Redirect, remove metasapiens page methods

I am looking to replace Metasapiens PageMethods, essentially this creates a class that allows for runtime checking of the link,

eg

Response.Redirect(MyPageMethods.Web.Display.Customer(custId));

This would go the the display page, and then call the customer method with the customer id. However I want to update to .Net 4.5 and the development of PageMethods stopped in 2007.
( http://metasapiens.com/pagemethods/ )

Therefore is there any best practices / easy ways of making my pages redirect to links which can be compiler checked as such when the project is built?

In case anyone ever finds this, I made it so that I created a quick application that ran through the xml file the program generated, and created classes that had a url and all the methods within it for example:

namespace PS.Web.Pages.Maintenance.Area.Display
{
    public static class DisplayArea
    {
        public static string URL = "/Pages/Maintenance/Area/Display/DisplayArea.aspx";

        public static string Show(System.Int32 areaId)
        {
            return string.Format("{0}?PageMethod=Show&areaId={1}", URL, areaId);
        }
    }
}

Then you just need to add something into a base page so that on page load we can initiliase the page, and pass in the parameters

protected void Page_Load(object sender, EventArgs e) 
{
    InitiatePage();
}

private void InitiatePage()
{
    var queryString = Request.QueryString;

    var pageMethodObject = queryString["PageMethod"];
    if (pageMethodObject != null)
    {
        string methodName = pageMethodObject.ToString();

        Type ty = this.GetType().BaseType;

        MethodInfo methodInfo = ty.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);

        if (methodInfo != null)
        {
            ParameterInfo[] parameters = methodInfo.GetParameters();

            if (parameters.Length == 0)
            {
                methodInfo.Invoke(this, null);
            }
            else
            {
                IList<object> parametersArray = new List<object>();

                foreach (var parameter in parameters)
                {
                    var value = queryString[parameter.Name];
                    object obj = null;
                    if (parameter.ParameterType.IsEnum)
                    {
                        obj = Enum.Parse(parameter.ParameterType, value);
                    }
                    else
                    {
                        obj = Convert.ChangeType(value, parameter.ParameterType);
                    }
                    parametersArray.Add(obj);
                }

                //try and run the method
                methodInfo.Invoke(this, parametersArray.ToArray());
            }
        }
    }
}

now it should automatically all link up.... So now the metasapiens page methods are gone and we don't have to worry about them! - Obviously when we create out classes with the URL buried in the parameters MUST match the parameters of the methods we are calling into else this will not work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM