sta*_*ium 5 asp.net url response.redirect
我正在寻找一种旨在减少ASP.NET应用程序中硬编码的URL数量的最佳实践解决方案.
例如,在查看产品详细信息屏幕,对这些详细信息执行编辑,然后提交更改时,用户将被重定向回产品列表屏幕.而不是编码以下内容:
Response.Redirect("~/products/list.aspx?category=books");
Run Code Online (Sandbox Code Playgroud)
我想有一个解决方案,允许我做这样的事情:
Pages.GotoProductList("books");
Run Code Online (Sandbox Code Playgroud)
where Pages
是公共基类的成员.
我只是在这里吐痰,并且很想听到任何人管理他们的应用程序重定向的任何其他方式.
编辑
我最终创建了以下解决方案:我已经有了一个公共基类,我添加了一个Pages枚举(感谢Mark),每个项目都有一个System.ComponentModel.DescriptionAttribute
包含页面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
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一些重载方法来处理不同的场景.我使用反射通过它的Description
属性获取页面的URL ,并将查询字符串参数作为匿名类型传递(也使用反射将每个属性添加为查询字符串参数):
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)) as 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));
}
Run Code Online (Sandbox Code Playgroud)
那么典型的电话会是这样的:
GotoPage(Enums.Pages.Landing, new {id = 12, category = "books"});
Run Code Online (Sandbox Code Playgroud)
评论?
我建议您从 Page 类派生自己的类(“MyPageClass”)并在其中包含此方法:
public class MyPageClass : Page
{
private const string productListPagePath = "~/products/list.aspx?category=";
protected void GotoProductList(string category)
{
Response.Redirect(productListPagePath + category);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在您的代码隐藏中,确保您的页面派生自此类:
public partial class Default : MyPageClass
{
...
}
Run Code Online (Sandbox Code Playgroud)
在其中,您可以使用以下命令进行重定向:
GotoProductList("Books");
Run Code Online (Sandbox Code Playgroud)
现在,这有点有限,因为您无疑会有各种其他页面,例如 ProductList 页面。您可以在页面类中为它们中的每一个提供自己的方法,但这有点糟糕并且不能顺利扩展。
我通过保留一个带有页面名称/文件名映射的数据库表来解决类似的问题(我正在调用外部动态添加的HTML文件,而不是ASPX文件,所以我的需求有点不同,但我认为原理申请)。然后,您的调用将使用字符串,或者更好的是,使用枚举来重定向:
protected void GoToPage(PageTypeEnum pgType, string category)
{
//Get the enum-to-page mapping from a table or a dictionary object stored in the Application space on startup
Response.Redirect(GetPageString(pgType) + category); // *something* like this
}
Run Code Online (Sandbox Code Playgroud)
在您的页面上,您的调用将是:GoToPage(enumProductList, "Books");
好处是,调用是对祖先类中定义的函数(不需要传递或创建管理器对象),并且路径非常明显(如果您使用枚举,智能感知将限制您的范围)。
祝你好运!
归档时间: |
|
查看次数: |
1538 次 |
最近记录: |