url - ASP.NET - Avoid hardcoding paths -
i'm looking best practice solution aims reduce amount of urls hard-coded in asp.net application.
for example, when viewing product details screen, performing edit on these details, , submitting changes, user redirected product listing screen. instead of coding following:
response.redirect("~/products/list.aspx?category=books");
i have solution in place allows me this:
pages.gotoproductlist("books");
where pages
member of common base class.
i'm spit-balling here, , love hear other way in has managed application redirects.
edit
i ended creating following solution: had common base class, added pages enum (thanks mark), each item having system.componentmodel.descriptionattribute
attribute containing page's url:
public enum pages { [description("~/secure/default.aspx")] landing, [description("~/secure/modelling/default.aspx")] modellinghome, [description("~/secure/reports/default.aspx")] reportshome, [description("~/error.aspx")] error }
then created few overloaded methods handle different scenarios. used reflection url of page through it's description
attribute, , pass query-string parameters anonymous type (also using reflection add each property query-string parameter):
private string getenumdescription(enum value) { type type = value.gettype(); string name = enum.getname(type, value); if (name != null) { fieldinfo field = type.getfield(name); if (field != null) { descriptionattribute attr = attribute.getcustomattribute(field, typeof(descriptionattribute)) descriptionattribute; if (attr != null) return attr.description; } } return null; } protected string getpageurl(enums.pages target, object variables) { var sb = new stringbuilder(); sb.append(urlhelper.resolveurl(helper.getenumdescription(target))); if (variables != null) { sb.append("?"); var properties = (variables.gettype()).getproperties(); foreach (var property in properties) sb.append(string.format("{0}={1}&", property.name, property.getvalue(variables, null))); } return sb.tostring(); } protected void gotopage(enums.pages target, object variables, bool usetransfer) { if(usetransfer) httpcontext.current.server.transfer(getpageurl(target, variables)); else httpcontext.current.response.redirect(getpageurl(target, variables)); }
a typical call so:
gotopage(enums.pages.landing, new {id = 12, category = "books"});
comments?
i'd suggest derive own class ("mypageclass") page class , include method there:
public class mypageclass : page { private const string productlistpagepath = "~/products/list.aspx?category="; protected void gotoproductlist(string category) { response.redirect(productlistpagepath + category); } }
then, in codebehind, make sure page derives class:
public partial class default : mypageclass { ... }
within that, can redirect using:
gotoproductlist("books");
now, bit limited since you'll undoubtedly have variety of other pages productlist page. could give each 1 of them own method in page class kind of grody , not smoothly extensible.
i solve problem kind of keeping db table page name/file name mapping in (i'm calling external, dynamically added html files, not aspx files needs bit different think principles apply). call use either string or, better yet, enum redirect:
protected void gotopage(pagetypeenum pgtype, string category) { //get enum-to-page mapping table or dictionary object stored in application space on startup response.redirect(getpagestring(pgtype) + category); // *something* }
from page call be: gotopage(enumproductlist, "books");
the nice thing call function defined in ancestor class (no need pass around or create manager objects) , path pretty obvious (intellisense limit ranges if use enum).
good luck!
Comments
Post a Comment